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.

92 lines
2.0 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. wiType :: WiredIn -> NFType
  16. wiType WiType = VType
  17. wiType WiPretype = VTypeω
  18. wiType WiInterval = VTypeω
  19. wiType WiI0 = VI
  20. wiType WiI1 = VI
  21. wiType WiIAnd = VI ~> VI ~> VI
  22. wiType WiIOr = VI ~> VI ~> VI
  23. wiType WiINot = VI ~> VI
  24. wiValue :: WiredIn -> Value
  25. wiValue WiType = VType
  26. wiValue WiPretype = VTypeω
  27. wiValue WiInterval = VI
  28. wiValue WiI0 = VI0
  29. wiValue WiI1 = VI1
  30. wiValue WiIAnd = fun \x -> fun \y -> iand x y
  31. wiValue WiIOr = fun \x -> fun \y -> ior x y
  32. wiValue WiINot = fun inot
  33. (~>) :: Value -> Value -> Value
  34. a ~> b = VPi P.Ex a (Closure "_" (const b))
  35. infixr 7 ~>
  36. fun :: (Value -> Value) -> Value
  37. fun k = VLam P.Ex $ Closure "x" (k . force)
  38. dprod :: Value -> (Value -> Value) -> Value
  39. dprod a b = VPi P.Ex a (Closure "x" b)
  40. wiredInNames :: Map Text WiredIn
  41. wiredInNames = Map.fromList
  42. [ ("pretype", WiPretype)
  43. , ("type", WiType)
  44. , ("interval", WiInterval)
  45. , ("i0", WiI0)
  46. , ("i1", WiI1)
  47. , ("iand", WiIAnd)
  48. , ("ior", WiIOr)
  49. , ("inot", WiINot)
  50. ]
  51. newtype NoSuchPrimitive = NoSuchPrimitive { getUnknownPrim :: Text }
  52. deriving (Show, Typeable)
  53. deriving anyclass (Exception)
  54. -- Interval operations
  55. iand, ior :: Value -> Value -> Value
  56. iand = \case
  57. VI1 -> id
  58. VI0 -> const VI0
  59. x -> \case
  60. VI0 -> VI0
  61. VI1 -> x
  62. y -> VIAnd x y
  63. ior = \case
  64. VI0 -> id
  65. VI1 -> const VI1
  66. x -> \case
  67. VI1 -> VI1
  68. VI0 -> x
  69. y -> VIOr x y
  70. inot :: Value -> Value
  71. inot = \case
  72. VI0 -> VI1
  73. VI1 -> VI0
  74. VIOr x y -> VIAnd (inot x) (inot y)
  75. VIAnd x y -> VIOr (inot x) (inot y)
  76. VINot x -> x
  77. x -> VINot x