2

In the following program we know that valStr.value assumes the subtype pair of the generic type t. Yet when I examine it in poly the type is shown as being t. Is there any way I can see in the poly interpreter that t has been specialized to pair?

This is what I get when I run poly :

> poly

Poly/ML 5.5.2 Release

> use "forum.ml";

signature PAIR =
  sig val coord : pair val getFirst : pair -> real type pair end
structure Pair :
  sig
    val coord : pair
    val getFirst : pair -> real
    type pair = real * real
  end
signature VALUE = sig type t val value : t end
functor createVal (R : PAIR) : VALUE
val extracted = 1.0: real
val main = fn: unit -> unit
structure valStr : VALUE
val it = (): unit

> valStr.value;
val it = (1.0, 2.0): valStr.t

(* I want to see that it is of type "pair" *)

and the code used to produce it is :

(* forum.ml *)
signature PAIR = 
  sig 
    type pair
    val coord : pair
    val getFirst : pair -> real
  end

structure Pair = 
  struct 
    type pair = real * real
    val coord = ((1.0,2.0) : pair)
    fun getFirst ((x,y) : pair):real = x
  end

signature VALUE =
  sig
    type t
    val value: t
  end

functor createVal(R : PAIR) : VALUE =
  struct
    type t = R.pair
    val value = R.coord
  end

structure valStr = createVal(Pair)

val extracted = Pair.getFirst valStr.value
fun main() = print (Real.toString extracted)
artella
  • 5,068
  • 4
  • 27
  • 35

1 Answers1

3

You can add a type constraint:

> valStr.value: Pair.pair;
val it = (1.0, 2.0): Pair.pair

Poly/ML tries to print the type in a helpful way but it can't guess which will be the most helpful in any particular case when there are multiple equivalent types.

David Matthews
  • 516
  • 2
  • 1
  • But I want to be able to work out, **using the interpreter**, what the type of `valStr.value` is, without knowing its type a-priori. If, using your suggestion, I put in a wrong type `valStr.value : real` I simply get `Static Errors`. Wouldn't it be possible to have polyml output **all** possible inferred type instead of `Static Errors`? Thanks – artella Aug 30 '15 at 12:34
  • 1
    Just realised I can do `val temp : real = valStr.value;` and it gives a helpful error message `Reason: Can't unify real to real * real (Incompatible types)` which tells me that `valStr.value` is of type `real * real` – artella Aug 30 '15 at 12:58