less prototype, less bad code implementation of CCHM type theory
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

197 lines
7.6 KiB

  1. {-# OPTIONS_GHC -Wno-orphans #-}
  2. {-# LANGUAGE ViewPatterns #-}
  3. module Syntax.Pretty where
  4. import Control.Arrow (Arrow(first))
  5. import qualified Data.Map.Strict as Map
  6. import qualified Data.Text.Lazy as L
  7. import qualified Data.Set as Set
  8. import qualified Data.Text as T
  9. import Data.Map.Strict (Map)
  10. import Data.Text (Text)
  11. import Data.Set (Set)
  12. import Data.Generics
  13. import Presyntax.Presyntax (Plicity(..))
  14. import Prettyprinter.Render.Terminal
  15. import Prettyprinter
  16. import Syntax
  17. instance Pretty Name where
  18. pretty (Bound x _) = pretty x
  19. pretty (Defined x _) = pretty x
  20. prettyTm :: Term -> Doc AnsiStyle
  21. prettyTm = prettyTm . everywhere (mkT beautify) where
  22. prettyTm (Ref v) =
  23. case T.uncons (getNameText v) of
  24. Just ('.', w) -> keyword (pretty w)
  25. _ -> pretty v
  26. prettyTm (App Im f x) = parenFun f <+> braces (prettyTm x)
  27. prettyTm (App Ex f x) = parenFun f <+> parenArg x
  28. prettyTm (Pair x y) = parens $ prettyTm x <> operator comma <+> prettyTm y
  29. prettyTm (Proj1 x) = prettyTm x <> operator (pretty ".1")
  30. prettyTm (Proj2 x) = prettyTm x <> operator (pretty ".2")
  31. prettyTm l@(Lam _ _ _) = pretty '\\' <> hsep (map prettyArgList al) <+> pretty "->" <+> prettyTm bod where
  32. unwindLam (Lam p x y) = first ((p, x):) (unwindLam y)
  33. unwindLam (PathIntro _ _ _ (Lam p x y)) = first ((p, x):) (unwindLam y)
  34. unwindLam t = ([], t)
  35. (al, bod) = unwindLam l
  36. used = freeVars bod
  37. prettyArgList (Ex, v)
  38. | v `Set.member` used = pretty v
  39. | otherwise = pretty "_"
  40. prettyArgList (Im, v)
  41. | v `Set.member` used = braces $ pretty v
  42. | otherwise = pretty "_"
  43. prettyTm (Meta x) = keyword $ pretty '?' <> pretty (mvName x)
  44. prettyTm Type{} = keyword $ pretty "Type"
  45. prettyTm Typeω{} = keyword $ pretty "Typeω"
  46. prettyTm I{} = keyword $ pretty "I"
  47. prettyTm I0{} = keyword $ pretty "i0"
  48. prettyTm I1{} = keyword $ pretty "i1"
  49. prettyTm (Pi Ex (T.unpack . getNameText -> "_") d r) = prettyDom d <+> pretty "->" <+> prettyTm r
  50. prettyTm (Pi Im v d r) = group (braces (pretty v <+> colon <+> prettyTm d)) <> softline <> pretty "->" <+> prettyTm r
  51. prettyTm (Pi Ex v d r) = group (parens (pretty v <+> colon <+> prettyTm d)) <> softline <> pretty "->" <+> prettyTm r
  52. prettyTm (Sigma (T.unpack . getNameText -> "_") d r) = prettyDom d <+> pretty "*" <+> prettyTm r
  53. prettyTm (Sigma v d r) = parens (pretty v <+> colon <+> prettyTm d) <+> pretty "*" <+> prettyTm r
  54. prettyTm (IAnd x y) = parens $ prettyTm x <+> operator (pretty "&&") <+> prettyTm y
  55. prettyTm (IOr x y) = parens $ prettyTm x <+> operator (pretty "||") <+> prettyTm y
  56. prettyTm (INot x) = operator (pretty '~') <> prettyTm x
  57. prettyTm (System xs) = braces (mempty <+> hsep (punctuate comma (map go (Map.toList xs))) <+> mempty) where
  58. go (f, t) = prettyTm f <+> operator (pretty "=>") <+> prettyTm t
  59. prettyTm x = error (show x)
  60. beautify (PathP l x y) = toFun "PathP" [l, x, y]
  61. beautify (IElim _ _ _ f i) = App Ex f i
  62. beautify (PathIntro _ _ _ f) = f
  63. beautify (IsOne phi) = toFun "IsOne" [phi]
  64. beautify ItIsOne = Ref (Bound (T.pack ".1=1") 0)
  65. beautify (IsOne1 phi) = toFun "isOne1" [phi]
  66. beautify (IsOne2 phi) = toFun "isOne2" [phi]
  67. beautify Bool = Ref (Bound (T.pack ".Bool") 0)
  68. beautify Tt = Ref (Bound (T.pack ".true") 0)
  69. beautify Ff = Ref (Bound (T.pack ".false") 0)
  70. beautify (If a b c d) = toFun "if" [a, b, c, d]
  71. beautify (Lam Ex v (App Ex f (Ref v')))
  72. | v == v', v `Set.notMember` freeVars f = f
  73. beautify (Partial phi a) = toFun "Partial" [phi, a]
  74. beautify (PartialP phi a) = toFun "PartialP" [phi, a]
  75. beautify (Comp a phi u a0) = toFun "comp" [a, phi, u, a0]
  76. beautify (Sub a phi u) = toFun "Sub" [a, phi, u]
  77. beautify (Inc _ _ u) = toFun "inS" [u]
  78. beautify (Ouc _ _ _ u) = toFun "outS" [u]
  79. beautify (GlueTy a I1 _ _) = a
  80. beautify (GlueTy a b c d) = toFun "Glue" [a,b,c,d]
  81. beautify (Glue a b c d e f) = toFun "glue" [a,b,c,d,e,f]
  82. beautify (Unglue a b c d e) = toFun "unglue" [a,b,c,d,e]
  83. beautify x = x
  84. toFun s a = foldl (App Ex) (Ref (Bound (T.pack ('.':s)) 0)) a
  85. keyword :: Doc AnsiStyle -> Doc AnsiStyle
  86. keyword = annotate (color Magenta)
  87. operator :: Doc AnsiStyle -> Doc AnsiStyle
  88. operator = annotate (color Yellow)
  89. parenArg :: Term -> Doc AnsiStyle
  90. parenArg x@App{} = parens (prettyTm x)
  91. parenArg x@IElim{} = parens (prettyTm x)
  92. parenArg x@IsOne{} = parens $ prettyTm x
  93. parenArg x@IsOne1{} = parens $ prettyTm x
  94. parenArg x@IsOne2{} = parens $ prettyTm x
  95. parenArg x@Partial{} = parens $ prettyTm x
  96. parenArg x@PartialP{} = parens $ prettyTm x
  97. parenArg x@Sub{} = parens $ prettyTm x
  98. parenArg x@Inc{} = parens $ prettyTm x
  99. parenArg x@Ouc{} = parens $ prettyTm x
  100. parenArg x@Comp{} = parens $ prettyTm x
  101. parenArg x = prettyDom x
  102. prettyDom :: Term -> Doc AnsiStyle
  103. prettyDom x@Pi{} = parens (prettyTm x)
  104. prettyDom x@Sigma{} = parens (prettyTm x)
  105. prettyDom x = parenFun x
  106. parenFun :: Term -> Doc AnsiStyle
  107. parenFun x@Lam{} = parens $ prettyTm x
  108. parenFun x@PathIntro{} = parens $ prettyTm x
  109. parenFun x = prettyTm x
  110. render :: Doc AnsiStyle -> Text
  111. render = L.toStrict . renderLazy . layoutSmart defaultLayoutOptions
  112. showValue :: Value -> String
  113. showValue = L.unpack . renderLazy . layoutSmart defaultLayoutOptions . prettyTm . quote
  114. showFace :: Map Head Bool -> Doc AnsiStyle
  115. showFace = hsep . map go . Map.toList where
  116. go (h, b) = parens $ prettyTm (quote (VNe h mempty)) <+> operator (pretty "=") <+> pretty (if b then "i1" else "i0")
  117. freeVars :: Term -> Set Name
  118. freeVars (Ref v) = Set.singleton v
  119. freeVars (App _ f x) = Set.union (freeVars f) (freeVars x)
  120. freeVars (Pi _ n x y) = Set.union (freeVars x) (n `Set.delete` freeVars y)
  121. freeVars (Lam _ n x) = n `Set.delete` freeVars x
  122. freeVars (Let ns x) = Set.union (freeVars x `Set.difference` bound) freed where
  123. bound = Set.fromList (map (\(x, _, _) -> x) ns)
  124. freed = foldr (\(_, x, y) -> Set.union (Set.union (freeVars x) (freeVars y))) mempty ns
  125. freeVars Meta{} = mempty
  126. freeVars Type{} = mempty
  127. freeVars Typeω{} = mempty
  128. freeVars I{} = mempty
  129. freeVars I0{} = mempty
  130. freeVars I1{} = mempty
  131. freeVars (Sigma n x y) = Set.union (freeVars x) (n `Set.delete` freeVars y)
  132. freeVars (Pair x y) = Set.unions $ map freeVars [x, y]
  133. freeVars (Proj1 x) = Set.unions $ map freeVars [x]
  134. freeVars (Proj2 x) = Set.unions $ map freeVars [x]
  135. freeVars (IAnd x y) = Set.unions $ map freeVars [x, y]
  136. freeVars (IOr x y) = Set.unions $ map freeVars [x, y]
  137. freeVars (INot x) = Set.unions $ map freeVars [x]
  138. freeVars (PathP x y z) = Set.unions $ map freeVars [x, y, z]
  139. freeVars (IElim x y z a b) = Set.unions $ map freeVars [x, y, z, a, b]
  140. freeVars (PathIntro x y z a) = Set.unions $ map freeVars [x, y, z, a]
  141. freeVars (IsOne a) = Set.unions $ map freeVars [a]
  142. freeVars (IsOne1 a) = Set.unions $ map freeVars [a]
  143. freeVars (IsOne2 a) = Set.unions $ map freeVars [a]
  144. freeVars ItIsOne{} = mempty
  145. freeVars (Partial x y) = Set.unions $ map freeVars [x, y]
  146. freeVars (PartialP x y) = Set.unions $ map freeVars [x, y]
  147. freeVars (System fs) = foldr (\(x, y) -> Set.union (Set.union (freeVars x) (freeVars y))) mempty (Map.toList fs)
  148. freeVars (Sub x y z) = Set.unions $ map freeVars [x, y, z]
  149. freeVars (Inc x y z) = Set.unions $ map freeVars [x, y, z]
  150. freeVars (Ouc x y z a) = Set.unions $ map freeVars [x, y, z, a]
  151. freeVars (Comp x y z a) = Set.unions $ map freeVars [x, y, z, a]
  152. freeVars (GlueTy x y z a) = Set.unions $ map freeVars [x, y, z, a]
  153. freeVars (Glue x y z a b c) = Set.unions $ map freeVars [x, y, z, a, b, c]
  154. freeVars (Unglue x y z a c) = Set.unions $ map freeVars [x, y, z, a, c]
  155. freeVars Bool{} = mempty
  156. freeVars Tt{} = mempty
  157. freeVars Ff{} = mempty
  158. freeVars (If x y z a) = Set.unions $ map freeVars [x, y, z, a]