PPP Exercises: Expoiting Persistence


Demo: Standalone application

A standalone-application in Tycoon is a function 'main' that takes an array of strings as parameter and returns an integer value (see interface 'machineenv/Main.ti'). This function can be stored as boot file with 'dynamic.bootFile' (from stdenv). On creation of a store a boot file can be given by the parameter '-boot FILE'. This boot file, i.e. the main function in this file, is loaded an executed by the Tycoon Machine. The arguments given after the parameter '-user ARGS' are handed to the main function in the parameter array. Try this script from the Unix command shell:
tycoon -restart
import dynamic print;
let main(argv :Array(String)) :Int =
begin
  print.string("Hello again!\n")
  for i = 0 upto extent(argv)-1 do (* print all arguments *)
    print.int(i)
    print.string(": ")
    print.string(argv[i])
    print.ln()
  end
  print.string("Bye bye!\n")
  0 (* return value *)
end; (* main *)
dynamic.bootFile(main "main.x"); (* the main function is written *)
do exit; (* exit Tycoon *)
tycoon -create -store main.ts -boot main.x
tycoon -create -store main.ts -boot main.x -user First String "Second String"
You get all the parameters accepted by the Tycoon Machine with 'tycoon -help'.

Note that the Tycoon compiler is also only a main function. It is located in the file 'boot/tycoon.x' that is loaded per default if the parameter '-boot' is missing. The options of the Tycoon compiler are shown by the following command: 'tycoon -create -store test.ts -user -help'.

Demo: Persistent standalone application

In the example above the main function is started again and again in a 'fresh' (i.e. empty) store. This is exactly the behaviour of 'normal' (i.e. transient) applications like C-programs that have no persistent 'memory'. In Tycoon all data, functions and threads that are reachable can be stored persistent. This is done by the command 'do saveSystem;' on the top-level of the Tycoon compiler. In a standalone application this is be done by the function 'checkpoint.commit' (from machineenv). Try this:
tycoon -restart
import dynamic print checkpoint;
let main(argv :Array(String)) :Int =
begin
  print.string("Hello first time!\n")
  if checkpoint.commit() == checkpoint.commitVia then
    print.string("Commit.\n")
  else
    print.string("Hello again!\n")
  end
  print.string("Bye bye!\n")
  0 (* return value *)
end; (* main *)
dynamic.bootFile(main "main.x"); (* the main function is written *)
do exit; (* exit Tycoon *)
tycoon -create -store main.ts -boot main.x
tycoon -restart -store main.ts
tycoon -restart -store main.ts

Exercise: Persistent Counter

Write a main function that counts how often it has been restarted and prints the new counter value on restart. Test with:
tycoon -create -store counter.ts -boot counter.x
tycoon -restart -store counter.ts
tycoon -restart -store counter.ts
tycoon -restart -store counter.ts

Exercise: Parameterized Persistent Counter

Extend the counter function so that it gets its initial value from the command line on the first invocation. On each following restart it reads the increment from the command line (see the module 'parameters' from machineenv for getting the actual parameters on restart). Use the function 'convert' from exercise Using modules. Be careful with error checking: exactly one parameter has to be given; the parameter should be at least one and at most five digits long; only digits are allowed. Test with:
tycoon -create -store counter.ts -boot counter.x -user
tycoon -create -store counter.ts -boot counter.x -user 10 11
tycoon -create -store counter.ts -boot counter.x -user 10AB
tycoon -create -store counter.ts -boot counter.x -user ""
tycoon -create -store counter.ts -boot counter.x -user 1890191
tycoon -create -store counter.ts -boot counter.x -user 99999
tycoon -restart -store counter.ts -user
tycoon -restart -store counter.ts -user 10 11
tycoon -restart -store counter.ts -user 10AB
tycoon -restart -store counter.ts -user ""
tycoon -restart -store counter.ts -user 1890191
tycoon -restart -store counter.ts -user 2
tycoon -restart -store counter.ts -user 111

Exercise: Persistent application

Build an application that stores the data of students and their lectures. Define commands to
Gerald Schröder, 1996