Last Updated: 2018-09-09 Sun 17:28

CSCI 2041 Lab01: OCaml Basics

CODE DISTRIBUTION: lab01-code.zip

  • Download the code distribution every lab
  • See further setup instructions below

CHANGELOG:

Sun Sep 9 17:27:28 CDT 2018
Fixed an accidentally omitted line about reloading an altered source file via a #use ... directive. For details see Post 17's follow-ups.

1 Rationale

Every programming environment will have the means to compile and execute code. OCaml has several ways to do this including a interactive REPL (read-eval-print loop), a byte code compiler, and a native code compiler. Most programming environments also have mechanisms to split usable code across several source files and OCaml is no exception. A means to merge the source files into a single program is then required.

Completing this lab will cover the above concepts as well as introducing the basic structure of OCaml programs.

Associated Reading / Preparation

Grading Policy

  • Check-off 30%: Demonstrate to a TA that a student understands answers to questions. This must be done in person in groups of one or two. Check-offs can happen during the lab period of during a TA office hour.
  • Submit 70%: Submit required files according to the lab instruction. This can be done at any time and from anywhere with a network connection. Submitting does not require attending lab. All students must submit files even if they were checked off in a group during lab.

See the full policy in the syllabus.

2 Codepack

The codepack for the lab contains the following files:

File State Description
QUESTIONS.txt Edit Answer questions in the file to complete the lab
repl-v-compile/ Directory Problem 1 Directory
print_stuff.ml Edit Problem 1 file to analyze and edit
separate-compilation/ Directory Problem 2 Directory
defs_and_main.ml Provided Problem 2 file to analyze
defs_only.ml Provided Problem 2 file to analyze
main_only.ml Provided Problem 2 file to analyze

3 Questions

Analyze the files in the provided codepack and answer the questions given in QUESTIONS.txt.

			   __________________

			    LAB 01 QUESTIONS
			   __________________


- Name: (FILL THIS in)
- NetID: (THE kauf0095 IN kauf0095@umn.edu)

Answer the questions below according to the lab specification. Write
your answers directly in this text file and submit it to complete the
lab.


PROBLEM 1: OCaml's REPL
=======================

(A)
~~~

  In a Unix terminal, change into the directory `lab01-code' and then
  into the subdirectory `repl-v-compile'.  This can be done using the
  change directory command `cd' as in
  ,----
  | > cd lab01-code
  | > cd repl-v-compile
  `----

  In this directory should be an OCaml source file named
  `print_stuff.ml'.  You can verify it is there by using the listing
  command `ls' as in
  ,----
  | > ls
  | ... print_stuff.ml ...
  `----

  Start the OCaml REPL by typing the command `ocaml' which should change
  the prompt as follows.
  ,----
  | > ocaml
  | 	OCaml version 4.06.0
  | 
  | #
  `----
  The version reported may be different by in most cases this will not
  matter.

  Load the source file `print_stuff.ml' into the REPL with the `#use'
  directive as follows:
  ,----
  | > ocaml
  | 	OCaml version 4.06.0
  | 
  | # #use "print_stuff.ml";;
  | ....
  `----

  Copy and paste the results of doing this into the space below as your
  answer to this problem.


(B)
~~~

  Top-level statements in OCaml files can define values which are
  registered with the REPL when loading files. These will be output
  lines starting with `val' such as
  ,----
  | val student : string = "Meatbag #43281"
  `----
  One can verify that a name is bound to a value by typing the name
  followed by two semicolons and then enter as in
  ,----
  | # student;;
  | - : string = "Meatbag #43281"
  `----
  This reports the type of value bound to a name and its value.

  What other name is bound in the `print_stuff.ml' file? Give its name
  and type below and describe it to the best of your ability after
  examining the source file.


(C)
~~~

  One can create new name/value bindings in the REPL using `let x =
  ...;;' and override old bindings as well. Create the following
  bindings in the REPL:
  - Bind `life' to the integer 42
  - Bind `pie' to the floating point number 3.14159
  - Bind `student' to your name surrounded by quotes (this overrides the
    previous binding for `student'
  Show the code you used to create these bindings in the REPL


(D)
~~~

  Reload the source file by typing
  ,----
  | # #use "print_stuff.ml";;
  `----
  Determine what value is associated with names `life', `pie' and
  `student'. Have any changed?


(E)
~~~

  In a text editor, alter `print_stuff.ml' so that it has the bindings
  from the previous part as in:
  - Bind `life' to the integer 42
  - Bind `pie' to the floating point number 3.14159
  - Bind `student' to your name surrounded by quotes; alter the existing
    binding for this
  Make sure to save your changes in the text editor.

  QUIT the REPL by issuing the `#quit;;' directive then start it again
  and load the altered source file. Show the output below to verify that
  the changes you made have taken effect.


PROBLEM 2: Compiling OCaml to Byte-Code
=======================================

(A)
~~~

  Quit the REPL or open a new terminal and navigate to the
  `lab01-code/repl-v-compile' directory.

  Once in the `repl-v-compile' directory, issue the following command to
  compile the source file `print_stuff.ml' to byte-code.

  ,----
  | > ocamlc print_stuff.ml
  `----

  Verify that there are now a new set of files in the directory by
  issuing an `ls' command. Also issue a `file *' command which will
  report the type of all files in the directory.

  Copy the results of your commands below.


(B)
~~~

  The traditional default name for compiled programs in Unix is `a.out'
  which should be present after compiling.  You can run this program by
  typing `./a.out' as in
  ,----
  | > ./a.out
  `----

  The initial "dot slash" means "in the current directory". Without it,
  your shell will search for a program named `a.out' in standard
  directories like `/usr/bin' and probably not find one.

  Show the results of running the `a.out' program below.


(C)
~~~

  The output from the program will be somewhat different from the
  results of loading the same file in OCaml's REPL.  Describe these
  differences and explain why certain lines appear in both while others
  appear only in the REPL.


PROBLEM 3: Basic Style of OCaml Programs
========================================

(A)
~~~

  Navigate in the terminal to the `lab01-code/separate-compilation'.  To
  move up a directory in a Unix terminal, use the command `cd ..' so a
  sequence of commands like the following may be useful.
  ,----
  | > pwd                                     # show me what directory I'm in
  | /home/kau0095/lab01-code/repl-v-compile   # in the problem 1 directory
  | > cd ..                                   # change up one directory
  | > ls                                      # show what's in this directory
  | QUESTIONS.txt  repl-v-compile  separate-compilation
  | > cd separate-compilation                 # change to the problem 2 directory
  | > ls                                      # show what's in this directory
  | defs_and_main.ml   defs_only.ml  main_only.ml
  `----

  Examine the source file `defs_and_main.ml' in a text editor. This file
  represents the style we will follow for self-contained OCaml source
  files.

  Compile and run this program via the following commands.
  ,----
  | > ocamlc defs_and_main.ml
  | > ./a.out
  | ...
  `----

  Paste the output you get for the run below.


(B)
~~~

  Describe the basic structure of the `defs_and_main.ml' which is broken
  into roughly 3 sections. What is in each section?


(C)
~~~

  Now examine the source file `defs_only.ml'.
  - Describe how it differs from `defs_and_main.ml'
  - Compile it and run the resulting program. Describe anything odd
    about the output


(D)
~~~

  Examine the source file `main_only.ml' and note how it differs from
  `defs_and_main.ml'.  Pay particular attention to the first section of
  the file which will differ slightly.  Attempt to compile this to a
  program and show the result.


(E)
~~~

  Unlike `defs_and_main.ml', `main_only.ml' depends on the contents of
  the `defs_only.ml' file.  There are several ways that one can compile
  them together.  The most straight-forward is to simply give both files
  on the command line as shown below.
  ,----
  | > ocamlc defs_only.ml main_only.ml
  | > ./a.out
  | Bonjour!
  | Welcome to OCaml, Triangle Lover
  | A right triangle has sides length 4.0 and 3.0
  | Its hypotenuse has length 5.0
  | Au revoir!
  `----

  Note that order of the files on the command line matters as the
  compiler needs to compile `main_only.ml' after it has produced the
  results for `defs_only.ml' so the following produces an error.
  ,----
  | > ocamlc main_only.ml defs_only.ml 
  | File "defs_only.ml", line 1:
  | Error: Required module `Defs_only' is unavailable
  | >
  `----

  Finally, any run of the compiler can be instructed to name the output
  program something aside from `a.out'. This can be done with the `-o'
  option as shown below where the output program is named
  `triangle_greeter'.
  ,----
  | > ocamlc -o triangle_greeter defs_only.ml main_only.ml
  | > ./triangle_greeter
  | Bonjour!
  | Welcome to OCaml, Triangle Lover
  | A right triangle has sides length 4.0 and 3.0
  | Its hypotenuse has length 5.0
  | Au revoir!
  `----

  Choose a different output name and compile the files `defs_only.ml'
  and `main_only.ml' together to produce a program. Show your results
  below.

4 What to Understand

Ensure that you understand

  • The basic structure of an OCaml source file including top-level definitions and code that defines values functions versus code that is executed when a module is loaded.
  • How to start an interactive OCaml REPL, type basic program code into it, load files, and observe output.
  • How to compile OCaml source files to executables and run them including splitting program definitions across several files.

5 Getting Credit: Check-off and Turn-in

  • Check-off your answers with a TA in person during lab or office hours.
  • Submit your completed QUESTIONS.txt file to Canvas under the appropriate lab heading. Make sure to paste in any new code and answers you were to write at appropriate locations in the file.

Author: Chris Kauffman (kauffman@umn.edu)
Date: 2018-09-09 Sun 17:28