"Hello, world!" on ATS
ATS compiler is needed to run ATS program. In other words, ATS language doesn't have interpreter such like OCaml language. We try to compile some simple ATS program on this chapter, before learn full-featured ATS programming.
ATS program to do nothing
What is the minimum program? It's mostly a program has nothing without a empty main function. Let's create such "ATS program to do nothing".
.. literalinclude:: code/helloworld/nothing.dats :language: ocaml :linenos:
This is the ATS program has a main function doing nothing. Detail of the code is explained as following:
- Any ATS programs start at
implementdefines a new function
main0at the code
main0function has been declared in ATS language implementation
main0has no arguments and return value is
- The body of the
main0function does nothing and return
()is the only value of
Compile and run it
Let's compile the "ATS program to do nothing".
Save it as
nothing.dats and run following commands:
.. literalinclude:: code/helloworld/nothing_compile.txt :language: shell :linenos:
a.out is created by ATS compiler
Of course, the executable prints nothing on console while running.
By the way, how does the
patscc command work?
As in the figure, the
patscc internally calls two commands.
The first command is
patsopt that compile ATS language code to C language,
patsopt does typecheck the ATS code and reports type errors.
The compilation ATS to C is aborted, if typechecking found some type errors.
The second command is
gcc that is familiar to everyone.
C language code created by
patsopt is compiled to executable by the
Print character string on console
We learn how to use
patscc command in the previous sub chapter.
Let's try "Hello, world!" program.
.. literalinclude:: code/helloworld/helloworld.dats :language: ocaml :linenos:
This code includes following new items:
valdefines binding between name and value
valat the code binds function return value of the function
println!is a special keyword that prints the argument on console and return
Then, the code prints the character string
"Hello, world!" on console.
.. literalinclude:: code/helloworld/helloworld_compile.txt :language: shell :linenos:
This result may sound strange for a beginner of functional programming who has already learned imperative programming. Conceptually, what happens at run-time in a call-by-value language such as ATS is following:
- The expression
println! "Hello, world!"is evaluated to the value
(), while it prints the character string
"Hello, world!"on console
valcreates a bind between
()at left side of
=and the result value of the expression
println! "Hello, world!"
- The function
main0is evaluated and does nothing
The "Hello, world!" program can be written in
main0 function using
where keyword, as following:
.. literalinclude:: code/helloworld/helloworld2.dats :language: ocaml :linenos:
main0 function returns
void value, we can drop
() where as following:
.. literalinclude:: code/helloworld/helloworld3.dats :language: ocaml :linenos:
More simply, we can alos write following, because
void value as same as the value will be returned by
.. literalinclude:: code/helloworld/helloworld4.dats :language: ocaml :linenos: