home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / gofer230.zip / Progs / Gofer / Docs / ch07 < prev    next >
Text File  |  1994-06-23  |  16KB  |  463 lines

  1.  
  2.  
  3. Introduction to Gofer                                 7. BUILT-IN TYPES
  4.  
  5.  
  6. 7. BUILT-IN TYPES
  7.  
  8. An important part of Gofer is the type system which is used  to  detect
  9. errors  in  expressions  and  function  definitions.    Starting   with
  10. primitive expressions such as numeric constants, Gofer assigns  a  type
  11. to each expression that describes the kind of value represented by  the
  12. expression.
  13.  
  14. In  general  we write  object :: type  to indicate  that  a  particular
  15. expression has the indicated type.  For example:
  16.  
  17.     42   :: Int         indicating that  42  is an  integer (Int is the
  18.                         name for the type of integer values).
  19.  
  20.     fact :: Int -> Int  indicating  that  "fact"  is a  function  which
  21.                         takes  an  integer  argument  and  returns   an
  22.                         integer value (its factorial).
  23.  
  24. The most important property of the type system is that it  is  possible
  25. to determine the type of an expression without having to  evaluate  it.
  26. For example, the information given above  is  sufficient  to  determine
  27. that fact 42 :: Int without needing to calculate 42! first.
  28.  
  29. Gofer has a wide range of built-in types, described  in  the  following
  30. sections.  In addition, Gofer also includes facilities for defining new
  31. types as well as types acting as  abbreviations  for  complicated  type
  32. expressions as described in section 11.
  33.  
  34.  
  35. 7.1  Functions
  36. --------------
  37. If t1 and t2 are types then t1 -> t2 is the type of a  function  which,
  38. given an argument of type t1 produces a result of type t2.  A  function
  39. of type t1 -> t2 is said to have argument type t1 and result type t2.
  40.  
  41. In mathematics, the result of applying a function f to an argument x is
  42. traditionally written as f(x).  In many situations,  these  parentheses
  43. are unnecessary and may be omitted when using Gofer.
  44.  
  45. e.g. if f :: t1 -> t2 and x :: t1 then f x is the result of applying
  46.      f to x and has type t2.
  47.  
  48.  
  49. If t1, t2, ..., tn are type expressions then:
  50.  
  51.                    t1 -> t2 -> ... -> tn
  52.  
  53. can be used as an abbreviation for the type:
  54.  
  55.                t1 -> (t2 -> ( ... -> tn) ...)
  56.  
  57. In a similar way, an expression of the form f x1 x2 ... xn is simply an
  58. abbreviation for the expression ( ... ((f x1) x2) ... xn).
  59.  
  60. These two conventions allow us to deal with functions taking more  than
  61. one argument rather elegantly.  For example, the type of  the  addition
  62.  
  63.  
  64.                                       12
  65.  
  66.  
  67.  
  68.  
  69. Introduction to Gofer                                    7.1  Functions
  70.  
  71.  
  72. function (+) is:
  73.                        Int -> Int -> Int
  74.  
  75. In other words, "(+)" is a function which takes an integer argument and
  76. returns a value of type (Int -> Int).  For  example,  "(+)  5"  is  the
  77. function which takes an integer value n and returns the  value  of  the
  78. integer n plus 5.  Hence "(+) 5 4", which is equivalent  to  "5  +  4",
  79. evaluates to the integer 9 as expected.
  80.  
  81.  
  82. 7.2  Booleans
  83. -------------
  84. Represented by the type "Bool", there are two boolean values written as
  85. "True" and "False".   The  standard  prelude  includes  several  useful
  86. functions for manipulating boolean values:
  87.  
  88.     (&&), (||) :: Bool -> Bool -> Bool
  89.  
  90.         The value of the expression b && d is True if and only if  both
  91.         b and d are True.  If b is False then d is not evaluated.
  92.  
  93.         The value of the expression b || d is True if either of b or  d
  94.         is True.  If b is True then d is not evaluated.
  95.  
  96.     not  :: Bool -> Bool
  97.  
  98.         The value of the expression not b is the opposite boolean value
  99.         to that of b; not True = False, not False = True.
  100.  
  101. Gofer includes a special form of `conditional expression' which enables
  102. an expression to select between two alternatives according to the value
  103. of a boolean expression:
  104.  
  105.                      if b then t else f 
  106.  
  107. is an expression which is equivalent to t if b evaluates to True, or to
  108. f if b evaluates to False.  Note that an expression  of  this  form  is
  109. only acceptable if b is an expression of type Bool and if the types  of
  110. t and f are the same, in which case the whole expression also has  that
  111. type.
  112.  
  113.  
  114. 7.3  Integers
  115. -------------
  116. Represented by the type "Int", the integer type includes both  positive
  117. and negative integers such as -273, 0  and  383.   Like  many  computer
  118. systems, the range of integer values that can be used is restricted and
  119. calculations using large positive  or  negative  numbers  may  lead  to
  120. (undetected) overflow.
  121.  
  122. A wide range of operators and functions are  defined  in  the  standard
  123. prelude for use with integers:
  124.  
  125.     (+)     addition.
  126.     (*)     multiplication.
  127.     (-)     subtraction.
  128.  
  129.  
  130.                                       13
  131.  
  132.  
  133.  
  134.  
  135. Introduction to Gofer                                     7.3  Integers
  136.  
  137.  
  138.     (^)     raise to power.
  139.     negate  unary negation.  An expression of the form "-x" is treated
  140.             as the expression "negate x".
  141.     (/)     integer division.
  142.     div        "        "
  143.     rem     remainder, related to integer division by the law:
  144.                      (x `div` y)*y + (x `rem` y) == x
  145.     mod     modulo, like remainder except that the modulo has the same
  146.             sign as the divisor.
  147.     odd     returns True if argument is odd, False otherwise.
  148.     even    returns True if argument is even, False otherwise.
  149.     gcd     returns the greatest common divisor of its two arguments.
  150.     lcm     returns the least common multiple of its two arguments.
  151.     abs     returns the absolute value of its argument.
  152.     signum  returns -1, 0 or 1 indicating that its argument is negative,
  153.             zero or positive respectively.
  154.  
  155. The  less  familiar  operators  are  illustrated   by   the   following
  156. identities:
  157.  
  158.      3^4 == 81,          7 `div` 3 == 2,      even 23 == False
  159.      7 `rem` 3 == 1,    -7 `rem` 3 == -1,     7 `rem` -3 == 1
  160.      7 `mod` 3 == 1,    -7 `mod` 3 == 2,      7 `mod` -3 == -2
  161.      gcd 32 12 == 4,    abs (-2) == 2,        signum 12 == 1
  162.  
  163.  
  164. 7.4  Floating point numbers
  165. ---------------------------
  166. Represented by the type "Float", elements of this type can be  used  to
  167. represent fractional values  as  well  as  very  large  or  very  small
  168. quantities.  Such values are however usually only accurate to  a  fixed
  169. number of digits and rounding errors may  occur  in  some  calculations
  170. making significant use of floating point quantities.  A  numeric  value
  171. in an input expression will only be treated as a floating point  number
  172. if it either includes a decimal point such as 3.14159, or if the number
  173. is too large to be stored as a value of type Int.  Scientific  notation
  174. may also be used to enter floating point quantities; for example  1.0e3
  175. is equivalent to 1000.0, whilst 5.0e-2 is equivalent to 0.05.
  176.  
  177. [N.B. floating point numbers are not included in all implementations of
  178. Gofer].
  179.  
  180.  
  181. 7.5  Characters
  182. ---------------
  183. Represented by  the  type  "Char",  elements  of  this  type  represent
  184. individual characters such as those entered at a  keyboard.   Character
  185. values  are  written  as  single  characters  enclosed  by   apostrophe
  186. characters: e.g. 'a',  '0',  'Z'.   Some  special  characters  must  be
  187. entered using an `escape code'; each of these begins with  a  backslash
  188. character '\', followed  by  one  or  more  characters  to  select  the
  189. required character.  Some of the most useful escape codes are:
  190.  
  191.      '\\'             backslash
  192.      '\''             apostrophe
  193.      '\"'             double quote
  194.  
  195.  
  196.                                       14
  197.  
  198.  
  199.  
  200.  
  201. Introduction to Gofer                                   7.5  Characters
  202.  
  203.  
  204.      '\n'             newline
  205.      '\b' or '\BS'    backspace
  206.      '\DEL'           delete
  207.      '\t' or '\HT'    tab
  208.      '\a' or '\BEL'   alarm (bell)
  209.      '\f' or '\FF'    formfeed
  210.  
  211. Additional escape characters include:
  212.  
  213.      '\^c'            control character, where c is replaced by
  214.                       one of the characters:
  215.                          "@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_"
  216.                       For example, '\^A' represents control-A
  217.  
  218.      '\number'        representing the character with ASCII value
  219.                       specified by the given decimal 'number'.
  220.  
  221.      '\onumber'       representing the character with ASCII value
  222.                       specified by the given octal 'number'.
  223.  
  224.      '\xnumber'       representing the character with ASCII value
  225.                       specified by the given 'hexadecimal' number.
  226.  
  227.      '\name'          named ASCII control character, where
  228.                       `name' is replaced by one of the standard
  229.                       ascii names e.g. `\DC3`.
  230.  
  231. In contrast with  some  common  languages  (such  as  C,  for  example)
  232. character values are quite distinct from integers; however the standard
  233. prelude does include functions:
  234.  
  235.                    ord :: Char -> Int
  236.                    chr :: Int -> Char
  237.  
  238. which enable you to map a character to its corresponding  ASCII  value,
  239. or from an ASCII value to the corresponding character:
  240.  
  241.     ? ord 'a'
  242.     97
  243.     (2 reductions, 6 cells)
  244.     ? chr 65
  245.     'A'
  246.     (2 reductions, 7 cells)
  247.     ?         
  248.  
  249.  
  250. 7.6  Lists
  251. ----------
  252. If a is a type then [a] is the type whose elements are lists of  values
  253. of type a.  There are several ways of writing list expressions:
  254.  
  255.   o   The simplest list of any type is the empty list, written [].
  256.  
  257.   o   Non-empty lists  can be constructed either by  explicitly listing
  258.       the members of the list (for example: [1,3,10]) or  by  adding  a
  259.       single element onto the front  of  another  list  using  the  (:)
  260.  
  261.  
  262.                                       15
  263.  
  264.  
  265.  
  266.  
  267. Introduction to Gofer                                        7.6  Lists
  268.  
  269.  
  270.       operator (pronounced "cons").  These notations are equivalent:
  271.  
  272.        [1,3,10]  =  1 : [3,10]  =  1 : 3 : [10]  =  1 : 3 : 10 : []
  273.  
  274.       (the (:) operator groups to the right so 1 :  3  :  10  :  []  is
  275.       equivalent to (1:(3:(10:[]))) -- a list whose first element is 1,
  276.       second element is 3 and last element is 10).
  277.  
  278. The  standard  prelude  includes  a  wide  range   of   functions   for
  279. calculations involving lists.  For example:
  280.  
  281.   o  length xs  returns the number of elements in the list xs.
  282.   o  xs ++ ys   returns the list of elements in xs followed by the
  283.                 elements in ys
  284.   o  concat xss returns the list of elements in each of the lists in
  285.                 xss
  286.   o  map f xs   returns the list of values obtained by applying the
  287.                 function f to each of the values in the list xs in turn.
  288.  
  289. Here are some examples using these functions:
  290.  
  291.     ? length [1,3,10]
  292.     3
  293.     (15 reductions, 28 cells)
  294.  
  295.     ? [1,3,10] ++ [2,6,5,7]
  296.     [1, 3, 10, 2, 6, 5, 7]
  297.     (19 reductions, 77 cells)
  298.  
  299.     ? concat [[1], [2,3], [], [4,5,6]]
  300.     [1, 2, 3, 4, 5, 6]
  301.     (29 reductions, 93 cells)
  302.  
  303.     ? map ord ['H', 'e', 'l', 'l', 'o']
  304.     [72, 101, 108, 108, 111]
  305.     (22 reductions, 73 cells)
  306.  
  307.     ?
  308.  
  309. Note that all of the elements in a list must be of the  same  type,  so
  310. that an expression such as ['a', 2, False] is not permitted.
  311.  
  312. [ASIDE: At this point  it  might  be  useful  to  mention  an  informal
  313. convention that is used by a  number  of  functional  programmers  when
  314. choosing names for variables  representing  elements  of  lists,  lists
  315. themselves, lists of lists and  so  on.   If  for  example,  a  typical
  316. element of a list is called x, then it is often useful to use the  name
  317. xs for a list of such values, suggesting that a list contains a  number
  318. of "x"s.  Similarly, a list of lists might be  called  xss.   Once  you
  319. have understood this convention it  is  much  easier  to  remember  the
  320. relationship between the variables in the  expression  (x:xs)  than  it
  321. would be if different names had been used such as (a:b).]
  322.  
  323.  
  324.  
  325.  
  326.  
  327.  
  328.                                       16
  329.  
  330.  
  331.  
  332.  
  333. Introduction to Gofer                                      7.7  Strings
  334.  
  335.  
  336. 7.7  Strings
  337. ------------
  338. A string is treated as a list of characters  and  the  type  String  is
  339. simply an abbreviation for the type [Char].   Strings  are  written  as
  340. sequences of characters enclosed between  speech  marks.   All  of  the
  341. escape codes that can be used for characters may  also  be  used  in  a
  342. string:
  343.  
  344.     ? "hello, world"
  345.     hello, world
  346.     (0 reductions, 13 cells)
  347.  
  348.     ? "hello\nworld"
  349.     hello
  350.     world
  351.     (0 reductions, 12 cells)
  352.     ?
  353.  
  354. In addition, strings may contain the escape sequence "\&" which can  be
  355. used to separate otherwise  ambiguous  pairs  of  characters  within  a
  356. string:
  357.  
  358.     e.g.  "\123h"   represents the string ['\123', 'h']
  359.           "\12\&3h" represents the string ['\12', '3', 'h']
  360.  
  361. A string expression may be spread over a number of lines using a gap --
  362. a non-empty sequence of space, tab and new line characters enclosed  by
  363. backslash characters:
  364.  
  365.     ? "hell\   \o"
  366.     hello
  367.     (0 reductions, 6 cells)
  368.     ? 
  369.  
  370. Notice that strings are printed  differently from other  values,  which
  371. gives the programmer complete control over the  format  of  the  output
  372. produced by a program.  The only values that Gofer can in fact  display
  373. on the terminal are strings.  If the type of an expression entered into
  374. Gofer is equivalent to String then the expression is  printed  directly
  375. by evaluating and printing each character  in  the  list  in  sequence.
  376. Otherwise, the expression to  be  evaluated,  e,  is  replaced  by  the
  377. expression show' e where show' is a built-in function (defined as  part
  378. of the standard prelude)  which  converts  any  value  to  a  printable
  379. representation.  The  only way of printing a  string value  in the same
  380. way as any other value is by explicitly using the show' function:
  381.  
  382.     ? show' "hello"
  383.     "hello"
  384.     (7 reductions, 24 cells)
  385.     ?
  386.  
  387. The careful reader may have been puzzled by  the  fact  the  number  of
  388. reductions used in the first three examples above was zero.  This is in
  389. fact quite correct since these expressions are constants and no further
  390. evaluation can be carried out.  For constant expressions of  any  other
  391. type there will always be at least one reduction needed  to  print  the
  392.  
  393.  
  394.                                       17
  395.  
  396.  
  397.  
  398.  
  399. Introduction to Gofer                                      7.7  Strings
  400.  
  401.  
  402. value since the constant  must  first  be  translated  to  a  printable
  403. representation using the show' function.
  404.  
  405. Because strings are represented as lists  of  characters,  all  of  the
  406. standard prelude functions for manipulating lists can also be used with
  407. strings:
  408.  
  409.     ? length "Hello"
  410.     5
  411.     (22 reductions, 36 cells)
  412.  
  413.     ? "Hello, " ++ "world"
  414.     Hello, world
  415.     (8 reductions, 37 cells)
  416.  
  417.     ? concat ["super","cali","fragi","listic"]
  418.     supercalifragilistic
  419.     (29 reductions, 101 cells)
  420.  
  421.     ? map ord "Hello"
  422.     [72, 101, 108, 108, 111]
  423.     (22 reductions, 69 cells)
  424.  
  425.     ? 
  426.  
  427.  
  428. 7.8  Tuples and the unit type
  429. -----------------------------
  430. If t1, t2, ..., tn are types and n>=2, then there is a type of n-tuples
  431. written (t1, t2, ..., tn) whose elements are also written in  the  form
  432. (x1, x2, ..., xn) where the expressions x1, x2, ..., xn have types  t1,
  433. t2, ..., tn respectively.
  434.  
  435.     e.g.  (1, [2], 3)   :: (Int, [Int], Int)
  436.           ('a', False)  :: (Char, Bool)
  437.           ((1,2),(3,4)) :: ((Int, Int), (Int, Int))
  438.  
  439. Note that, unlike lists, the elements in a  tuple  may  have  different
  440. types, although the number of elements in the tuple is fixed.
  441.  
  442. The unit type is written () and has a  single  element  which  is  also
  443. written as ().  The unit type is of particular interest in  theoretical
  444. treatments of the type system of Gofer, although you  may  occasionally
  445. find a use for it in practical programs.
  446.  
  447.  
  448.  
  449.  
  450.  
  451.  
  452.  
  453.  
  454.  
  455.  
  456.  
  457.  
  458.  
  459.  
  460.                                       18
  461.  
  462.  
  463.