4. Basic data types

In the chapter, we learn some basic datatypes on ATS.

4.1. Integer

First, let’s create a text file named print_int.dats has following context:

1
2
3
4
(* File: print_int.dats *)
val () = println! 3

implement main0 () = ()

This program prints integer 3 on console. Then compile it.

$ patscc print_int.dats
$ ./a.out
3

You get the result just as our intended. The 3 in the print_int.dats file is a literal, and type of the value is int.

It’s available doing basic arithmetic operations on integers. Addition operator is +, subtraction is - and multiplication is *. let’s create a text file named int_op.dats has following context:

1
2
3
4
5
6
7
8
(* File: int_op.dats *)
#include "share/atspre_staload.hats"

val () = println! (3 + 4 * 2)
val () = println! ((3 + 4) * 2)
val () = println! (2 - 3)

implement main0 () = ()

The int_op.dats file includes share/atspre_staload.hats file that is needed to use ATS’s template. Some operators such like + are implemented using the template on ATS language implementation.

You will get following intended result, by compiling the code and running it.

$ patscc int_op.dats
$ ./a.out
11
14
-1

Multiplication and division are given priority over addition and subtraction, without parentheses.

You can also do division / and modulus %.

1
2
3
4
5
6
7
(* File: divmod_op.dats *)
#include "share/atspre_staload.hats"

val () = println! (7 / 2)
val () = println! (7 % 2)

implement main0 () = ()

Let’s compile and run it.

$ patscc divmod_op.dats
$ ./a.out
3
1

By the way, what happen if you do division with 0 as divisor on a new code divzero.dats?

1
2
3
4
5
6
(* File: divmod.dats *)
#include "share/atspre_staload.hats"

val () = println! (7 / (2 - 2))

implement main0 () = ()

Interestingly, divzero.dats occurs an error on compiling time, even though many programming language take care division with 0 as run-time error.

$ patscc divzero.dats
/tmp/divzero.dats: 84(line=4, offs=22) -- 85(line=4, offs=23): error(3): unsolved constraint: C3NSTRprop(main; S2Eapp(S2Ecst(!=); S2EVar(4145->S2Eapp(S2Ecst(sub_int_int); S2EVar(4141->S2Eintinf(2)), S2EVar(4142->S2Eintinf(2)))), S2Eintinf(0)))
typechecking has failed: there are some unsolved constraints: please inspect the above reported error message(s) for information.
exit(ATS): uncaught exception: _2home_2kiwamu_2src_2ATS_2dPostiats_2src_2pats_error_2esats__FatalErrorExn(1025)

However, the compilation error message is hard to understand for the reader on this chapter. I’ll explain detail of the error messages elsewhere.

Note

Exercise: Calculate following expressions on ATS language.

  1. \(7 - 3 \times 4\)
  2. \(7 \div 2 \times 2\)
  3. \(7 \times 2 \div 2\)

4.2. Real number

Literal of real number is represented using a decimal point, and the type is double.

1
2
3
4
(* File: print_double.dats *)
val () = println! 2.718

implement main0 () = ()

A value of real number is able to be printed using println!.

$ patscc print_double.dats
$ ./a.out
2.718000

Addition operator +, subtraction -, multiplication * and division / can be used on real number. Please see following double_op.dats:

1
2
3
4
5
6
(* File: double_op.dats *)
#include "share/atspre_staload.hats"

val () = println! ((3.0 + 5.0) * 8.0 / 3.0)

implement main0 () = ()
$ patscc double_op.dats
$ ./a.out
21.333333

Zeros after the decimal point are optional.

1
2
3
4
5
6
(* File: double_op_nozero.dats *)
#include "share/atspre_staload.hats"

val () = println! ((3. + 5.) * 8. / 3.)

implement main0 () = ()

ATS is different to OCaml, and can use operators between different types (such like int and double).

1
2
3
4
5
6
(* File: double_int_op.dats *)
#include "share/atspre_staload.hats"

val () = println! ((3 + 5.0) * 8 / 3.0)

implement main0 () = ()
$ patscc double_int_op.dats
$ ./a.out
21.333333

This mixing is based on ATS template system.

Note

Exercise: Calculate following expressions on ATS language.

  1. \(2 \times 3.14 \times 10\)
  2. \(1.73 \times 1.73\)
  3. \(7 / 2\)

4.3. Boolean value

Boolean value is a value for true or false. On ATS language, true is for true and false for false. Type of the boolean value is bool.

1
2
3
4
5
(* File: print_bool.dats *)
val () = println! true
val () = println! false

implement main0 () = ()
$ patscc print_bool.dats
$ ./a.out
true
false

We can use logical operations on the boolean value.x && is for “and”, || for “or” and not for “negation”, in the order of descending priorities.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
(* File: bool_op.dats *)
#include "share/atspre_staload.hats"

val () = println! (true && true)
val () = println! (true && false)
val () = println! (true || false)
val () = println! (false || false)
val () = println! (not true)
val () = println! (not false)
val () = println! (not (false || not false && not false))
val () = println! (not (false || (not false) && (not false)))

implement main0 () = ()
$ patscc bool_op.dats
$ ./a.out
true
false
true
false
false
true
false
false

true and false are rarely used on real programming. Normally, we use them to test whether some conditions hold. Examples of the test operators are: = is for “equality”, != for “inequality”, > for “greater than”, < for “less than”, >= for “greater than or equal”, and <= “less than or equal”.

1
2
3
4
5
6
7
8
9
(* File: bool_test.dats *)
#include "share/atspre_staload.hats"

val () = println! (2 < 3)
val () = println! (2 < 3 && 2.0 >= 3.0)
val () = println! (2 < 3 || 2.0 = 3.0)
val () = println! (not (3 < 2))

implement main0 () = ()
$ patscc bool_test.dats
$ ./a.out
true
false
true
true

Note

Exercise: Test conditions of following sentences on ATS language.

  1. \(2\) is greater than \(3\).”
  2. “The second power of \(3.1415\) is not less than \(10\).”
  3. “The remainder obtained by dividing \(8\) by \(3\) is equal to \(2\).”
  4. \(3 + 4 + 5\) is equal to \(4 \times 3\).”

4.4. The other data types

Character string and the other data types are explained later.