0

I am new to SML (meta-language). Can anyone tell me how to derive the function from the type given as below: (’a -> ’b) -> (’b list -> ’c) -> ’a -> ’c list

I am having a hard time in understanding the curried functions in SML.

Aniruddh Khera
  • 111
  • 1
  • 13
  • Can you clarify the question? There are an infinite number of functions that have that type. You can't give a type and then say "Give the function that has this type." There are an infinite number of them. Going the other way works, though. If you write a function in SML, then you can compute its type. Perhaps you are looking for _a_ function with that type? Is that what you are asking? – Ray Toal Nov 24 '17 at 22:37
  • Hi @RayToal, yes indeed. It can be any function, as long as it satisfies this type. Also, it would be great if you could give an explanation how to deduce function from its type. – Aniruddh Khera Nov 24 '17 at 22:45
  • As I can see that there are three different types: 'a 'b 'c I can write something like: fun foo a b c = b [a c]; It's not correct, but something like this, possibly? – Aniruddh Khera Nov 24 '17 at 22:49
  • 1
    Yes that is very close (as you can see from the answer below). Great start! – Ray Toal Nov 25 '17 at 18:33

1 Answers1

2

This will work

- fun f g h x = [h [g x]];
> val ('a, 'b, 'c) f = fn : ('a -> 'b) -> ('b list -> 'c) -> 'a -> 'c list

Here's how I did it.

We are given the type

('a -> 'b) -> ('b list -> 'c) -> 'a -> 'c list

So we know we want a function with three curried arguments, the first two are functions, and the third is anything. So we write:

fun f g h x = ....

Now the first argument is a function that takes in something of type 'a, which x is so we want a

g x

on the right hand side. That will be of type 'b. Now h takes in a 'b list, so we can write

h [g x]

This produces a value of type 'c, but we want f to return a 'c list so we just put that in a list, so we get:

fun f g h x = [h [g x]];
Ray Toal
  • 86,166
  • 18
  • 182
  • 232