首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在函子应用后,在polyml中查看泛型的专门化/子类型

在函子应用后,在polyml中查看泛型的专门化/子类型
EN

Stack Overflow用户
提问于 2015-08-30 06:23:15
回答 1查看 48关注 0票数 2

在下面的程序中,我们知道valStr.value假设泛型类型t的子类型t。然而,当我在poly中检查它时,类型显示为t。在poly解释器中,我能看到t专门化为pair吗?

这就是我运行poly时得到的结果:

代码语言:javascript
复制
> 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" *)

用来产生它的代码是:

代码语言:javascript
复制
(* 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)
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-08-30 11:36:43

可以添加类型约束:

代码语言:javascript
复制
> valStr.value: Pair.pair;
val it = (1.0, 2.0): Pair.pair

Poly试图以一种有用的方式打印该类型,但它无法猜测在任何特定情况下,当存在多个等效类型时,哪种类型最有用。

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/32293838

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档