Built-in.agda 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. -- This module introduces built-in types and primitive functions.
  2. module Introduction.Built-in where
  3. {- Agda supports four built-in types :
  4. - integers,
  5. - floating point numbers,
  6. - characters, and
  7. - strings.
  8. Note that strings are not defined as lists of characters (as is the case in
  9. Haskell).
  10. To use the built-in types they first have to be bound to Agda types. The
  11. reason for this is that there are no predefined names in Agda.
  12. -}
  13. -- To be able to use the built-in types we first introduce a new set for each
  14. -- built-in type.
  15. postulate
  16. Int : Set
  17. Float : Set
  18. Char : Set
  19. String : Set
  20. -- We can then bind the built-in types to these new sets using the BUILTIN
  21. -- pragma.
  22. {-# BUILTIN INTEGER Int #-}
  23. {-# BUILTIN FLOAT Float #-}
  24. {-# BUILTIN CHAR Char #-}
  25. {-# BUILTIN STRING String #-}
  26. pi : Float
  27. pi = 3.141593
  28. forAll : Char
  29. forAll = '∀'
  30. hello : String
  31. hello = "Hello World!"
  32. -- There are no integer literals. Instead there are natural number literals. To
  33. -- use these you have to tell the type checker which type to use for natural
  34. -- numbers.
  35. data Nat : Set where
  36. zero : Nat
  37. suc : Nat -> Nat
  38. {-# BUILTIN NATURAL Nat #-}
  39. -- Now we can define
  40. fortyTwo : Nat
  41. fortyTwo = 42
  42. -- To anything interesting with values of the built-in types we need functions
  43. -- to manipulate them. To this end Agda provides a set of primitive functions.
  44. -- To gain access to a primitive function one simply declares it. For instance,
  45. -- the function for floating point addition is called primFloatPlus. See below
  46. -- for a complete list of primitive functions. At the moment the name that you
  47. -- bring into scope is always the name of the primitive function. In the future
  48. -- we might allow a primitive function to be introduced with any name.
  49. module FloatPlus where -- We put it in a module to prevent it from clashing with
  50. -- the plus function in the complete list of primitive
  51. -- functions below.
  52. primitive
  53. primFloatPlus : Float -> Float -> Float
  54. twoPi = primFloatPlus pi pi
  55. -- Some primitive functions returns elements of non-primitive types. For
  56. -- instance, the integer comparison functions return booleans. To be able to
  57. -- use these functions we have to explain which type to use for booleans.
  58. data Bool : Set where
  59. false : Bool
  60. true : Bool
  61. {-# BUILTIN BOOL Bool #-}
  62. {-# BUILTIN TRUE true #-}
  63. {-# BUILTIN FALSE false #-}
  64. module FloatLess where
  65. primitive
  66. primFloatLess : Float -> Float -> Bool
  67. -- There are functions to convert a string to a list of characters, so we need
  68. -- to say which list type to use.
  69. data List (A : Set) : Set where
  70. nil : List A
  71. _::_ : A -> List A -> List A
  72. {-# BUILTIN LIST List #-}
  73. {-# BUILTIN NIL nil #-}
  74. {-# BUILTIN CONS _::_ #-}
  75. module StringToList where
  76. primitive
  77. primStringToList : String -> List Char
  78. -- Below is a partial version of the complete list of primitive
  79. -- functions.
  80. primitive
  81. -- Integer functions
  82. primIntegerPlus : Int -> Int -> Int
  83. primIntegerMinus : Int -> Int -> Int
  84. primIntegerTimes : Int -> Int -> Int
  85. primIntegerDiv : Int -> Int -> Int -- partial
  86. primIntegerMod : Int -> Int -> Int -- partial
  87. primIntegerEquality : Int -> Int -> Bool
  88. primIntegerLess : Int -> Int -> Bool
  89. primIntegerAbs : Int -> Nat
  90. primNatToInteger : Nat -> Int
  91. primShowInteger : Int -> String
  92. -- Floating point functions
  93. primIntegerToFloat : Int -> Float
  94. primFloatPlus : Float -> Float -> Float
  95. primFloatMinus : Float -> Float -> Float
  96. primFloatTimes : Float -> Float -> Float
  97. primFloatNegate : Float -> Float
  98. primFloatDiv : Float -> Float -> Float
  99. primFloatLess : Float -> Float -> Bool
  100. primRound : Float -> Int
  101. primFloor : Float -> Int
  102. primCeiling : Float -> Int
  103. primExp : Float -> Float
  104. primLog : Float -> Float -- partial
  105. primSin : Float -> Float
  106. primCos : Float -> Float
  107. primTan : Float -> Float
  108. primASin : Float -> Float
  109. primACos : Float -> Float
  110. primATan : Float -> Float
  111. primATan2 : Float -> Float -> Float
  112. primShowFloat : Float -> String
  113. -- Character functions
  114. primCharEquality : Char -> Char -> Bool
  115. primIsLower : Char -> Bool
  116. primIsDigit : Char -> Bool
  117. primIsAlpha : Char -> Bool
  118. primIsSpace : Char -> Bool
  119. primIsAscii : Char -> Bool
  120. primIsLatin1 : Char -> Bool
  121. primIsPrint : Char -> Bool
  122. primIsHexDigit : Char -> Bool
  123. primToUpper : Char -> Char
  124. primToLower : Char -> Char
  125. primCharToNat : Char -> Nat
  126. primNatToChar : Nat -> Char -- partial
  127. primShowChar : Char -> String
  128. -- String functions
  129. primStringToList : String -> List Char
  130. primStringFromList : List Char -> String
  131. primStringAppend : String -> String -> String
  132. primStringEquality : String -> String -> Bool
  133. primShowString : String -> String