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.

141 lines
3.5 KiB

  1. {-# LANGUAGE BlockArguments #-}
  2. {-# LANGUAGE LambdaCase #-}
  3. {-# LANGUAGE OverloadedStrings #-}
  4. {-# LANGUAGE DerivingStrategies #-}
  5. {-# LANGUAGE DeriveAnyClass #-}
  6. module Elab.WiredIn where
  7. import Syntax
  8. import Data.Map.Strict (Map)
  9. import Data.Text (Text)
  10. import qualified Data.Map.Strict as Map
  11. import Control.Exception
  12. import Data.Typeable
  13. import qualified Presyntax.Presyntax as P
  14. import Elab.Eval
  15. import qualified Data.Sequence as Seq
  16. wiType :: WiredIn -> NFType
  17. wiType WiType = VType
  18. wiType WiPretype = VTypeω
  19. wiType WiInterval = VTypeω
  20. wiType WiI0 = VI
  21. wiType WiI1 = VI
  22. wiType WiIAnd = VI ~> VI ~> VI
  23. wiType WiIOr = VI ~> VI ~> VI
  24. wiType WiINot = VI ~> VI
  25. wiType WiPathP = dprod (VI ~> VTypeω) \a -> a @@ VI0 ~> a @@ VI1 ~> VType
  26. wiType WiIsOne = VI ~> VType
  27. wiType WiItIsOne = VIsOne VI1
  28. wiType WiIsOne1 = forAll VI \i -> forAll VI \j -> VIsOne i ~> VIsOne (ior i j)
  29. wiType WiIsOne2 = forAll VI \i -> forAll VI \j -> VIsOne j ~> VIsOne (ior i j)
  30. wiType WiPartial = VI ~> VType ~> VTypeω
  31. wiType WiPartialP = dprod VI \x -> VPartial x VType ~> VTypeω
  32. wiValue :: WiredIn -> Value
  33. wiValue WiType = VType
  34. wiValue WiPretype = VTypeω
  35. wiValue WiInterval = VI
  36. wiValue WiI0 = VI0
  37. wiValue WiI1 = VI1
  38. wiValue WiIAnd = fun \x -> fun \y -> iand x y
  39. wiValue WiIOr = fun \x -> fun \y -> ior x y
  40. wiValue WiINot = fun inot
  41. wiValue WiPathP = fun \a -> fun \x -> fun \y -> VPath a x y
  42. wiValue WiIsOne = fun VIsOne
  43. wiValue WiItIsOne = VItIsOne
  44. wiValue WiIsOne1 = forallI \_ -> forallI \_ -> fun VIsOne1
  45. wiValue WiIsOne2 = forallI \_ -> forallI \_ -> fun VIsOne2
  46. wiValue WiPartial = fun \phi -> fun \r -> VPartial phi r
  47. wiValue WiPartialP = fun \phi -> fun \r -> VPartialP phi r
  48. (~>) :: Value -> Value -> Value
  49. a ~> b = VPi P.Ex a (Closure "_" (const b))
  50. infixr 7 ~>
  51. fun :: (Value -> Value) -> Value
  52. fun k = VLam P.Ex $ Closure "x" (k . force)
  53. forallI :: (Value -> Value) -> Value
  54. forallI k = VLam P.Im $ Closure "x" (k . force)
  55. dprod :: Value -> (Value -> Value) -> Value
  56. dprod a b = VPi P.Ex a (Closure "x" b)
  57. forAll :: Value -> (Value -> Value) -> Value
  58. forAll a b = VPi P.Im a (Closure "x" b)
  59. wiredInNames :: Map Text WiredIn
  60. wiredInNames = Map.fromList
  61. [ ("Pretype", WiPretype)
  62. , ("Type", WiType)
  63. , ("Interval", WiInterval)
  64. , ("i0", WiI0)
  65. , ("i1", WiI1)
  66. , ("iand", WiIAnd)
  67. , ("ior", WiIOr)
  68. , ("inot", WiINot)
  69. , ("PathP", WiPathP)
  70. , ("IsOne", WiIsOne)
  71. , ("itIs1", WiItIsOne)
  72. , ("isOneL", WiIsOne1)
  73. , ("isOneR", WiIsOne2)
  74. , ("Partial", WiPartial)
  75. , ("PartialP", WiPartialP)
  76. ]
  77. newtype NoSuchPrimitive = NoSuchPrimitive { getUnknownPrim :: Text }
  78. deriving (Show, Typeable)
  79. deriving anyclass (Exception)
  80. -- Interval operations
  81. iand, ior :: Value -> Value -> Value
  82. iand = \case
  83. VI1 -> id
  84. VI0 -> const VI0
  85. VIAnd x y -> \case
  86. VI0 -> VI0
  87. VI1 -> VI1
  88. z -> iand x (iand y z)
  89. x -> \case
  90. VI0 -> VI0
  91. VI1 -> x
  92. y -> VIAnd x y
  93. ior = \case
  94. VI0 -> id
  95. VI1 -> const VI1
  96. VIOr x y -> \case
  97. VI1 -> VI1
  98. VI0 -> VIOr x y
  99. z -> ior x (ior y z)
  100. x -> \case
  101. VI1 -> VI1
  102. VI0 -> x
  103. y -> VIOr x y
  104. inot :: Value -> Value
  105. inot = \case
  106. VI0 -> VI1
  107. VI1 -> VI0
  108. VIOr x y -> VIAnd (inot x) (inot y)
  109. VIAnd x y -> VIOr (inot x) (inot y)
  110. VINot x -> x
  111. x -> VINot x
  112. ielim :: Value -> Value -> Value -> Value -> NFEndp -> Value
  113. ielim _line _left _right fn i =
  114. case force fn of
  115. VLine _ fun -> fun @@ i
  116. VNe n sp -> VNe n (sp Seq.:|> PIElim _line _left _right i)
  117. _ -> error $ "can't ielim " ++ show fn