Search code examples
functiontypesfunctional-programmingmiranda

How do I determine the data type for a program?


I'm trying to study for an exam, and I feel that the lecture slides confuse me a bit.

One of our test questions will likely be "What is the type of program programName", but I have trouble grasping the concept. I understand what data types are, but are you answering with the data type of the input, and tracing the program with the arrows?

Here's an example of something that confuses me. They want me to find the type of program p2:

p2 x y z = x y!z

The data type for p2 is:

(*->[**])->*->num->**

I don't understand how they came about that result, though. Is it:

  • The first star in the tuple representing the input x?
  • The second, two-star list in the tuple representing the input y?
  • The third star representing... something?
  • The num representing z?
  • The last two-star representing the output of the program?

I appreciate any advice!


Solution

  • * -> [**] is the type of x, * is the type of y, num the type of z and ** the type of the result.

    foo -> bar is the type of a function that takes a foo and returns a bar and [baz] is a list of bazs. * and ** (and any other number of *s) stand for an arbitrary type (with the restriction that if the same number of *s appears multiple times in a type, it stands for the same type each time.

    So x is a function that takes a value of an arbitrary type and produces a list of another arbitrary type; y is a value of x's argument type; z is a value of type num; and the result is a value of x's result type.