PPP Exercises: Subtyping on mutable values


Exercise: Subtyping of aggregates with mutable fields related to contravariance in function subtyping

Define a type 'PersonWork' as follows:
Let PersonWork = 
   Tuple
      hours :Int
      worker :Person
   end;
Is the following type a subtype of 'PersonWork'?
Let StudentWork = 
   Tuple
      hours :Int
      worker :Student
   end;
You can rearrange the above types as follows:
Let FunPersonWork = 
   Tuple
      getHours() :Int
      getWorker() :Person
   end;

Let FunStudentWork = 
   Tuple
      getHours() :Int
      getWorker() :Student
   end;
Of course, 'FunStudentWork' is a subtype of 'FunPersonWork'. A value of 'FunPersonWork' can be created in this way:
let pw :FunPersonWork =
   tuple
      fun() 3   
      fun() tuple "Ernie" 22 end 
   end;
Then, in order to access the tuple components you can use the dot notation:
pw.getHours();
pw.getWorker().name;
Now consider the same problem with the following types (i.e. insert the prefix 'Fun' and create access functions, using a get/set mechanism when a 'var' field occurs):
Let VarPersonWork1 =
   Tuple
      var hours :Int
      worker :Person
   end;

Let VarStudentWork1 =
   Tuple
      var hours :Int
      worker :Student
   end;

Let VarPersonWork2 = 
   Tuple
      hours :Int
      var worker :Person
   end;

Let VarStudentWork2 = 
   Tuple
      hours :Int
      var worker :Student
   end;
For example:
Let FunVarPersonWork1 = 
   Tuple
      getHours() :Int
      setHours(hours :Int) :Ok
      getWorker() :Person
   end;
Give a possible implementation for the access functions.

Is 'FunVarStudentWork1' a subtype of 'FunVarPersonWork1'?
Is 'FunVarStudentWork2' a subtype of 'FunVarPersonWork2'?

Use the knowledge coming from the exercise Subtyping and Contravariance in order to give the correct answer.

Also:
Is 'VarStudentWork1" a subtype of 'VarPersonWork1'?
Is 'VarStudentWork2' a subtype of 'VarPersonWork2'?


Ulrike Steffens, 1994