PageRenderTime 50ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/src/orc/lib/includes/prelude/core.inc

https://github.com/laurenyew/cOrcS
Pascal | 320 lines | 320 code | 0 blank | 0 comment | 0 complexity | dc872906d352ba94d6a1024a3de89a16 MD5 | raw file
Possible License(s): BSD-3-Clause
  1. --
  2. -- core.inc -- Orc standard prelude include, fundementals section
  3. -- Project OrcScala
  4. --
  5. -- $Id: core.inc 2933 2011-12-15 16:26:02Z jthywissen $
  6. --
  7. -- Copyright (c) 2011 The University of Texas at Austin. All rights reserved.
  8. --
  9. -- Use and redistribution of this file is governed by the license terms in
  10. -- the LICENSE file found in the project's top-level directory and also found at
  11. -- URL: http://orc.csres.utexas.edu/license.shtml .
  12. --
  13. {--
  14. Fundamental sites and operators.
  15. These declarations include both prefix and infix sites (operators). For
  16. consistency, all declarations are written in prefix form, with the site name
  17. followed by the operands. When the site name is surrounded in parentheses, as
  18. in <code>(+)</code>, it denotes an infix operator.
  19. For a more complete description of the built-in operators and their syntax, see
  20. the <link linkend="ref.expressions.operators">Operators</link> article.
  21. --}
  22. {-
  23. Basic types
  24. -}
  25. import type Top = "orc.types.Top"
  26. import type Bot = "orc.types.Bot"
  27. import type String = "orc.types.StringType"
  28. import type Number = "orc.types.NumberType"
  29. import type Boolean = "orc.types.BooleanType"
  30. import type Integer = "orc.types.IntegerType"
  31. import type Signal = "orc.types.SignalType"
  32. {--
  33. @site Let() :: Signal
  34. <od:sitepropset>
  35. <od:siteprop propname="definite"/>
  36. <od:siteprop propname="pure"/>
  37. </od:sitepropset>
  38. When called with no arguments, returns a <link linkend="ref.data.signal">signal</link>.
  39. @site Let(A) :: A
  40. When called with a single argument, returns that argument (behaving as the identity function).
  41. @site Let(A, ...) :: (A, ...)
  42. When called with two or more arguments, returns the arguments as a <link linkend="ref.data.tuple">tuple</link>.
  43. --}
  44. import site Let = "orc.lib.builtin.Let"
  45. {--
  46. @site Ift(Boolean) :: Signal
  47. <od:sitepropset>
  48. <od:siteprop propname="definite"/>
  49. <od:siteprop propname="pure"/>
  50. </od:sitepropset>
  51. Returns a <link linkend="ref.data.signal">signal</link> if the argument is true, otherwise <link linkend="ref.concepts.states.halt">halts</link> <link linkend="ref.concepts.silent">silently</link>.
  52. Example:
  53. <programlisting language="orc-demo"><![CDATA[
  54. -- Publishes: "Always publishes"
  55. Ift(false) >> "Never publishes"
  56. | Ift(true) >> "Always publishes"]]></programlisting>
  57. --}
  58. import site Ift = "orc.lib.builtin.Ift"
  59. {--
  60. @site Iff(Boolean) :: Signal
  61. <od:sitepropset>
  62. <od:siteprop propname="definite"/>
  63. <od:siteprop propname="pure"/>
  64. </od:sitepropset>
  65. Returns a <link linkend="ref.data.signal">signal</link> if the argument is false, otherwise <link linkend="ref.concepts.states.halt">halts</link> <link linkend="ref.concepts.silent">silently</link>.
  66. Example:
  67. <programlisting language="orc-demo"><![CDATA[
  68. -- Publishes: "Always publishes"
  69. Iff(false) >> "Always publishes"
  70. | Iff(true) >> "Never publishes"]]></programlisting>
  71. --}
  72. import site Iff = "orc.lib.builtin.Iff"
  73. {--
  74. @site Error(String) :: Bot
  75. <od:sitepropset>
  76. <od:siteprop propname="definite"/>
  77. </od:sitepropset>
  78. Emits the given string as an error message, then <link linkend="ref.concepts.states.halt">halt</link> <link linkend="ref.concepts.silent">silently</link>.
  79. Example, using <code>Error</code> to implement assertions:
  80. <programlisting language="orc-demo"><![CDATA[
  81. def assert(b) =
  82. if b then signal else Error("assertion failed")
  83. -- Fail with the error message: "assertion failed"
  84. assert(false)]]></programlisting>
  85. --}
  86. import site Error = "orc.lib.util.Error"
  87. {--
  88. @site (+)(Number, Number) :: Number
  89. <code>a+b</code> returns the sum of <code>a</code> and <code>b</code>.
  90. --}
  91. import site (+) = "orc.lib.math.Add"
  92. {--
  93. @site (-)(Number, Number) :: Number
  94. <code>a-b</code> returns the value of <code>a</code> minus the value of <code>b</code>.
  95. --}
  96. import site (-) = "orc.lib.math.Sub"
  97. {--
  98. @site (0-)(Number) :: Number
  99. Return the additive inverse of the argument.
  100. When this site appears as an operator, it is written in prefix form without the
  101. zero, i.e. <code>-a</code>
  102. --}
  103. import site (0-) = "orc.lib.math.UMinus"
  104. {--
  105. @site (*)(Number, Number) :: Number
  106. <code>a*b</code> returns the product of <code>a</code> and <code>b</code>.
  107. --}
  108. import site (*) = "orc.lib.math.Mult"
  109. {--
  110. @site (**)(Number, Number) :: Number
  111. <code>a ** b</code> returns
  112. <inlineequation><mathphrase>a<superscript>b</superscript></mathphrase></inlineequation>,
  113. i.e. <code>a</code> raised to the <code>b</code>th power.
  114. --}
  115. import site (**) = "orc.lib.math.Exponent"
  116. {--
  117. @site (/)(Number, Number) :: Number
  118. <code>a/b</code> returns <code>a</code> divided by <code>b</code>.
  119. If both arguments have integral types, <code>(/)</code> performs integral
  120. division, rounding towards zero. Otherwise, it performs floating-point
  121. division. If <code>b=0</code>, <code>a/b</code> <link linkend="ref.concepts.states.halt">halts</link> with an error.
  122. Example:
  123. <programlisting language="orc-demo"><![CDATA[
  124. 7/3 -- publishes 2
  125. | 7/3.0 -- publishes 2.333...]]></programlisting>
  126. --}
  127. import site (/) = "orc.lib.math.Div"
  128. {--
  129. @site (%)(Number, Number) :: Number
  130. <code>a%b</code> computes the remainder of <code>a/b</code>. If <code>a</code>
  131. and <code>b</code> have integral types, then the remainder is given by
  132. the expression <code>a - (a/b)*b</code>. For a full description, see the
  133. <link xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="http://java.sun.com/docs/books/jls/third_edition/html/expressions.html#15.17.3">Java Language Specification, 3rd edition</link>.
  134. --}
  135. import site (%) = "orc.lib.math.Mod"
  136. {--
  137. @site (<:)(Top, Top) :: Boolean
  138. <code>a &lt;: b</code> returns true if <code>a</code> is less than <code>b</code>, and false otherwise.
  139. --}
  140. import site (<:) = "orc.lib.comp.Less"
  141. {--
  142. @site (<=)(Top, Top) :: Boolean
  143. <code>a &lt;= b</code> returns true if <code>a</code> is less than or equal to <code>b</code>, and false otherwise.
  144. --}
  145. import site (<=) = "orc.lib.comp.Leq"
  146. {--
  147. @site (:>)(Top, Top) :: Boolean
  148. <code>a :> b</code> returns true if <code>a</code> is greater than <code>b</code>, and false otherwise.
  149. --}
  150. import site (:>) = "orc.lib.comp.Greater"
  151. {--
  152. @site (>=)(Top, Top) :: Boolean
  153. <code>a >= b</code> returns true if <code>a</code> is greater than or equal to <code>b</code>, and false otherwise.
  154. --}
  155. import site (>=) = "orc.lib.comp.Greq"
  156. {--
  157. @site (=)(Top, Top) :: Boolean
  158. <code>a = b</code> returns true if <code>a</code> is equal to <code>b</code>,
  159. and false otherwise. The precise definition of "equal" depends on the values
  160. being compared, but always obeys the rule that if two values are considered
  161. equal, then one may be substituted locally for the other without affecting the
  162. behavior of the program.
  163. Two values with the same object identity are always considered equal.
  164. Orc data structures, such as tuples, are equal if their contents are equal.
  165. Other types are free to implement their own equality
  166. relationship provided it conforms to the rules given here.
  167. Note that although values of different types may be compared with
  168. <code>=</code>, the substitutability principle requires that such values are
  169. always considered inequal, i.e. the comparison will return <code>false</code>.
  170. --}
  171. import site (=) = "orc.lib.builtin.Eq"
  172. {--
  173. @site (/=)(Top, Top) :: Boolean
  174. <code>a/=b</code> returns false if <code>a=b</code>, and true otherwise.
  175. --}
  176. import site (/=) = "orc.lib.comp.Inequal"
  177. {--
  178. @site (~)(Boolean) :: Boolean
  179. Return the logical negation of the argument.
  180. --}
  181. import site (~) = "orc.lib.bool.Not"
  182. {--
  183. @site (&&)(Boolean, Boolean) :: Boolean
  184. Return the logical conjunction of the arguments. This is not a short-circuiting
  185. operator; both arguments must publish before the result is
  186. computed.
  187. --}
  188. import site (&&) = "orc.lib.bool.And"
  189. {--
  190. @site (||)(Boolean, Boolean) :: Boolean
  191. Return the logical disjunction of the arguments. This is not a short-circuiting
  192. operator; both arguments must publish before the result is
  193. computed.
  194. --}
  195. import site (||) = "orc.lib.bool.Or"
  196. {--
  197. @site (:)[A](A, List[A]) :: List[A]
  198. The <link linkend="ref.data.list">list</link> <code>a:b</code> is formed by prepending the element <code>a</code> to
  199. the list <code>b</code>.
  200. Example:
  201. <programlisting language="orc-demo"><![CDATA[
  202. -- Publishes: (3, [4, 5])
  203. 3:4:5:[] >x:xs> (x,xs)]]></programlisting>
  204. --}
  205. import site (:) = "orc.lib.builtin.structured.ConsConstructor"
  206. {--
  207. @def abs(Number) :: Number
  208. Publishes the absolute value of the argument.
  209. @implementation
  210. --}
  211. def abs(Number) :: Number
  212. def abs(x) = if x <: 0 then -x else x
  213. {--
  214. @def signum(Number) :: Number
  215. <code>signum(a)</code> publishes <code>-1</code> if <code>a&lt;0</code>,
  216. <code>1</code> if <code>a&gt;0</code>, and <code>0</code> if <code>a=0</code>.
  217. @implementation
  218. --}
  219. def signum(Number) :: Number
  220. def signum(x) =
  221. if x <: 0 then -1
  222. else if x :> 0 then 1
  223. else 0
  224. {--
  225. @def min[A](A,A) :: A
  226. Publishes the lesser of the arguments. If the arguments
  227. are equal, publishes the first argument.
  228. @implementation
  229. --}
  230. def min[A](A,A) :: A
  231. def min(x,y) = if y <: x then y else x
  232. {--
  233. @def max[A](A,A) :: A
  234. Publishes the greater of the arguments. If the arguments
  235. are equal, publishes the second argument.
  236. @implementation
  237. --}
  238. def max[A](A,A) :: A
  239. def max(x,y) = if x :> y then x else y
  240. {--
  241. @site Floor(Number) :: Integer
  242. Return the greatest integer less than
  243. or equal to this number.
  244. --}
  245. import site Floor = "orc.lib.math.Floor"
  246. {--
  247. @site Ceil(Number) :: Integer
  248. Return the least integer greater than
  249. or equal to this number.
  250. --}
  251. import site Ceil = "orc.lib.math.Ceil"
  252. {--
  253. @def sqrt(Number) :: Integer
  254. Publish the square root of this number.
  255. If the number is negative, <link linkend="ref.concepts.states.halt">halt</link> <link linkend="ref.concepts.silent">silently</link>.
  256. @implementation
  257. --}
  258. def sqrt(Number) :: Number
  259. def sqrt(n) = n ** 0.5