在函子应用程序后查看 polyml 中的泛型 specialization/subtype

Viewing specialization/subtype of generic in polyml after functor application

在下面的程序中我们知道valStr.value 假定泛型 t 的子类型 pair。然而,当我在 poly 中检查它时,类型显示为 t。有什么方法可以让我在 poly 解释器中看到 t 已专门用于 pair

这是我在 运行 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" *)

用于生成它的代码是:

(* 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)

您可以添加类型约束:

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

Poly/ML 尝试以有用的方式打印类型,但它无法猜测在有多个等效类型的任何特定情况下哪个最有用。