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.

109 lines
2.6 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. wiValue :: WiredIn -> Value
  27. wiValue WiType = VType
  28. wiValue WiPretype = VTypeω
  29. wiValue WiInterval = VI
  30. wiValue WiI0 = VI0
  31. wiValue WiI1 = VI1
  32. wiValue WiIAnd = fun \x -> fun \y -> iand x y
  33. wiValue WiIOr = fun \x -> fun \y -> ior x y
  34. wiValue WiINot = fun inot
  35. wiValue WiPathP = fun \a -> fun \x -> fun \y -> VPath a x y
  36. (~>) :: Value -> Value -> Value
  37. a ~> b = VPi P.Ex a (Closure "_" (const b))
  38. infixr 7 ~>
  39. fun :: (Value -> Value) -> Value
  40. fun k = VLam P.Ex $ Closure "x" (k . force)
  41. forallI :: (Value -> Value) -> Value
  42. forallI k = VLam P.Im $ Closure "x" (k . force)
  43. dprod :: Value -> (Value -> Value) -> Value
  44. dprod a b = VPi P.Ex a (Closure "x" b)
  45. forAll :: Value -> (Value -> Value) -> Value
  46. forAll a b = VPi P.Im a (Closure "x" b)
  47. wiredInNames :: Map Text WiredIn
  48. wiredInNames = Map.fromList
  49. [ ("Pretype", WiPretype)
  50. , ("Type", WiType)
  51. , ("Interval", WiInterval)
  52. , ("i0", WiI0)
  53. , ("i1", WiI1)
  54. , ("iand", WiIAnd)
  55. , ("ior", WiIOr)
  56. , ("inot", WiINot)
  57. , ("PathP", WiPathP)
  58. ]
  59. newtype NoSuchPrimitive = NoSuchPrimitive { getUnknownPrim :: Text }
  60. deriving (Show, Typeable)
  61. deriving anyclass (Exception)
  62. -- Interval operations
  63. iand, ior :: Value -> Value -> Value
  64. iand = \case
  65. VI1 -> id
  66. VI0 -> const VI0
  67. x -> \case
  68. VI0 -> VI0
  69. VI1 -> x
  70. y -> VIAnd x y
  71. ior = \case
  72. VI0 -> id
  73. VI1 -> const VI1
  74. x -> \case
  75. VI1 -> VI1
  76. VI0 -> x
  77. y -> VIOr x y
  78. inot :: Value -> Value
  79. inot = \case
  80. VI0 -> VI1
  81. VI1 -> VI0
  82. VIOr x y -> VIAnd (inot x) (inot y)
  83. VIAnd x y -> VIOr (inot x) (inot y)
  84. VINot x -> x
  85. x -> VINot x
  86. ielim :: Value -> Value -> Value -> Value -> NFEndp -> Value
  87. ielim _line _left _right fn i =
  88. case force fn of
  89. VLine _ fun -> fun @@ i
  90. VNe n sp -> VNe n (sp Seq.:|> PIElim _line _left _right i)
  91. _ -> error $ "can't ielim " ++ show fn