debuggingsmlsubtypesubtypingpolyml

Viewing specialization/subtype of generic in polyml after functor application


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)

Solution

  • 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.