Echo2

Let us make the Echo program just a little bit more interesting: We add an initial welcome phrase and a prompt with a line number. Here is the program:


module Echo2 where

import POSIX

echo2 env = class

   count := 1

   prompt = do
      env.stdout.write (show count++"> ")
      count := count+1

   echo str = action
      env.stdout.write str
      prompt

   result
      action
         env.stdin.installR echo
         env.stdout.write "Welcome to Echo2!\n"
         prompt

root = newRoot echo2


The class definition now has two more components:

In addition, the interface returned upon instantiation is a dedicated method that takes care of all program-specific initialization. Such a class allows for a particularly simple root definition, as shown in the last line of the example. The wrapper function newRoot has the following definition in module POSIX:

newRoot :: (Env -> Class Action) -> World -> Cmd () ()
newRoot cl world = do
   env = new posix world
   init = new cl env
   init

Some additional details: