a type theory with equality based on setoids
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.

135 lines
4.0 KiB

  1. {-# LANGUAGE ViewPatterns #-}
  2. module Syntax.Pretty where
  3. import Syntax
  4. import Data.Text ( Text )
  5. import qualified Data.Text as T
  6. import Elaboration.Monad
  7. type Prec = Int
  8. domainPrec, funcPrec, argPrec :: Int
  9. domainPrec = 3
  10. argPrec = 2
  11. funcPrec = 1
  12. showWithPrec :: [Text] -> Int -> Term -> ShowS
  13. showWithPrec names p (App Ex x y) =
  14. showParen (p >= argPrec) $
  15. showWithPrec names funcPrec x
  16. . showChar ' '
  17. . showWithPrec names argPrec y
  18. showWithPrec names p (App Im x _) = showWithPrec names p x
  19. showWithPrec _ _ Type = showString "Type"
  20. showWithPrec _ _ Top = showString ""
  21. showWithPrec _ _ Unit = showString "()"
  22. -- Reflexivity
  23. showWithPrec _ _ Refl = showString "refl"
  24. -- Casting
  25. showWithPrec _ _ Coe = showString "coe"
  26. -- Congruence (x == y → f x == f y)
  27. showWithPrec _ _ Cong = showString "cong"
  28. -- Symmetry
  29. showWithPrec _ _ Sym = showString "sym"
  30. showWithPrec _ _ (Meta (MV i)) = showChar '?' . shows i
  31. showWithPrec _ _ (NewMeta (MV i) _) = showChar '?' . shows i
  32. showWithPrec names _ (Bv i) =
  33. if i < 0
  34. then showString "α"
  35. else showString (T.unpack (names !! i))
  36. showWithPrec names _ (Proj1 x) = showWithPrec names funcPrec x . showString ".1"
  37. showWithPrec names _ (Proj2 x) = showWithPrec names funcPrec x . showString ".2"
  38. showWithPrec names p (Lam i t e) =
  39. showParen (p >= funcPrec) $
  40. showChar 'λ'
  41. . showsPlicity i id (showString (T.unpack t))
  42. . showString ""
  43. . showWithPrec (t:names) 0 e
  44. showWithPrec names p (Pi Ex (T.unpack -> "_") d r) =
  45. showParen (p >= argPrec) $
  46. showWithPrec names domainPrec d
  47. . showString " -> "
  48. . showWithPrec (T.singleton '_':names) 0 r
  49. showWithPrec names p (Pi i n d r) =
  50. showParen (p >= argPrec) $
  51. showsPlicity i (showParen True)
  52. ( showString (T.unpack n)
  53. . showString " : "
  54. . showWithPrec names 0 d
  55. )
  56. . showString " -> "
  57. . showWithPrec (n:names) 0 r
  58. showWithPrec names p (Sigma (T.unpack -> "_") d r) =
  59. showParen (p >= argPrec) $
  60. showWithPrec names domainPrec d
  61. . showString " × "
  62. . showWithPrec (T.singleton '_':names) 0 r
  63. showWithPrec names p (Sigma n d r) =
  64. showParen (p >= argPrec) $
  65. showParen True
  66. ( showString (T.unpack n)
  67. . showString " : "
  68. . showWithPrec names 0 d
  69. )
  70. . showString " × "
  71. . showWithPrec (n:names) 0 r
  72. showWithPrec names _ (Pair a b) =
  73. showParen True $
  74. showWithPrec names 0 a
  75. . showString " , "
  76. . showWithPrec names 0 b
  77. showWithPrec names p (Id _ b c) =
  78. showParen (p >= funcPrec) $
  79. showWithPrec names argPrec b . showString " == " . showWithPrec names argPrec c
  80. showWithPrec names p (Let x t d e) =
  81. showParen (p >= funcPrec) $
  82. showString "let\n"
  83. . showString (" " ++ T.unpack x)
  84. . showString " : "
  85. . showWithPrec names 0 t
  86. . showChar '\n'
  87. . showString (" " ++ T.unpack x ++ " = ")
  88. . showWithPrec names 0 d
  89. . showString ";\n"
  90. . showWithPrec (x:names) 0 e
  91. showTerm :: Int -> Term -> ShowS
  92. showTerm = showWithPrec (iterate (`T.snoc` '\'') (T.pack "x"))
  93. showsPlicity :: Plicity -> (ShowS -> ShowS) -> ShowS -> ShowS
  94. showsPlicity Ex f k = f k
  95. showsPlicity Im _ k = showChar '{' . k . showChar '}'
  96. showElabError :: ElabError -> ShowS
  97. showElabError (NotInScope t) = showString "Variable not in scope: " . shows t
  98. showElabError (NotFunction names t) =
  99. showString "Type is not a function type: "
  100. . showWithPrec (names ++ exes) 0 t
  101. where
  102. exes = iterate (`T.snoc` '\'') (T.pack "x")
  103. showElabError (NotEqual names a b) =
  104. showString "Types are not equal:"
  105. . showString "\n * " . showWithPrec (names ++ exes) 0 a
  106. . showString "\n vs"
  107. . showString "\n * " . showWithPrec (names ++ exes) 0 b
  108. where
  109. exes = iterate (`T.snoc` '\'') (T.pack "x")
  110. showElabError (CantSolveMeta ns q t) =
  111. showString "Equation has no (unique) solution: "
  112. . showString "\n " . showWithPrec (ns ++ exes) 0 q
  113. . showString " ≡? " . showWithPrec (ns ++ exes) 0 t
  114. where
  115. exes = iterate (`T.snoc` '\'') (T.pack "x")