0

How many functions are present in this expression? :

'a -> 'a -> ('a*'a)

Also, how would you implement a function to return this type? I've created functions that have for example:

'a -> 'b -> ('a * b)

I created this by doing:

fun function x y = (x,y);

But I've tried using two x inputs and I get an error trying to output the first type expression.

Thanks for the help!

Chris
  • 785
  • 10
  • 24

2 Answers2

1

To be able to have two inputs of the same alpha type, I have to specify the type of both inputs to alpha.

E.g

fun function (x:'a) (y:'a) = (x, y);

==>

a' -> 'a -> (a' * 'a)
Chris
  • 785
  • 10
  • 24
  • This is a good answer. Without any assumptions on the type you really can't do anything other than form tuples whose elements are drawn from the passed 2. It is easy to see that there are only 4 distinct polymorphic pure functions of the given type. – John Coleman Nov 05 '15 at 14:42
1

Assuming this is homework, I don't want to say too much. -> in a type expression represents a function. 'a -> 'a -> ('a * 'a) has two arrows, so 2 might be the answer for your first question, though I find that particular question obscure. An argument could be made that each fun defines exactly one function, which might happen to return a function for its output. Also, you ask "how many functions are present in the expression ... " but then give a string which literally has 0 functions in it (type descriptions describe functions but don't contain functions), so maybe the answer is 0.

If you want a natural example of int -> int -> int * int, you could implement the functiondivmod where divmod x y returns a tuple consisting of the quotient and remainder upon dividing x by y. For example, you would want divmod 17 5 to return (3,2). This is a built-in function in Python but not in SML, but is easily defined in SML using the built-in operators div and mod. The resulting function would have a type of the form 'a -> 'a -> 'a*'a -- but for a specific type (namely int). You would have to do something which is a bit less natural (such as what you did in your answer to your question) to come up with a polymorphic example.

John Coleman
  • 51,337
  • 7
  • 54
  • 119