PPP Exercises: Emulating interfaces, modules and imports


Exercise:

  1. Write a function 'createIntStack' that takes any implementation 'funStack' of 'FunStack' and returns an implementation of 'IntStack' using 'funStack'.
    Let FunStack =
       Tuple
          T (E <:Ok) <:Ok
          new(E <:Ok) :T(E)
          empty(E <:Ok  stack :T(E)) :Bool
          push(E <:Ok  element :E  stack :T(E)) :T(E)
          pop(E <:Ok  stack :T(E)) :T(E)
          top(E <:Ok  stack :T(E)) :E
       end;
    
    Let IntStack =
       Tuple
          T <:Ok
          new() :T
          empty(stack :T) :Bool
          push(element :Int  stack :T) :T
          pop(stack :T) :T
          top(stack :T) :Int
       end;
    
    Test it with
    import list;
    
    let listStack :FunStack =
       tuple
          Let T(E <:Ok) <:Ok = list.T(E)
          let new(E <:Ok) :T(E) = list.new( :E)
          let empty(E <:Ok  stack :T(E)) :Bool =
              list.empty(stack)
          let push(E <:Ok  element :E  stack :T(E)) :T(E) =
              list.cons(element stack)
          let pop(E <:Ok  stack :T(E)) :T(E) = 
              list.tail(stack)
          let top(E <:Ok  stack :T(E)) :E = 
              list.head(stack)
       end;
    
    let intStack = createIntStack(listStack);
    
  2. Write a curried function 'createGenStack' that takes any implementation 'list' of 'List' and returns a polymorphic function like 'genStack' in the exercise Type operators and genericity that implements a stack using 'list'.
    Let GenStack(E <:Ok) <:Ok =
       Tuple
          T <:Ok
          new() :T
          empty(stack :T) :Bool
          push(element :E  stack :T) :T
          pop(stack :T) :T
          top(stack :T) :E
       end;
    
  3. Now think about the following questions, taking into account the previous solution:


Ulrike Steffens, 1994