Need and impossibility of having a type of a signature

泄露秘密 提交于 2019-12-04 20:33:29

First, a word on conventions : it's usually expected that module names have CamelCase formatting and module type names have ALL_UPPERCASE formatting. It took me two reads to notice you were dealing with module types instead of modules.

So, what you are trying to say here is that any module which implements module type MATRIX should be able, for any module Triangle that implements TRIANGLE, provide this signature:

type 'a t
val union : 'a Triangle.t -> 'a Triangle.t -> 'a t

It's not possible to express universal quantifiers like that. What you should do is use an existential quantifier and a functor :

module type MATRIX = sig
  module Triangle : TRIANGLE 
  type 'a t 
  val union : 'a Triangle.t -> 'a Triangle.t -> 'a t
end

module MatrixOfTriangle = functor (Triangle:TRIANGLE) -> struct
  module Triangle = Triangle
  type 'a t = ...
  let union t1 t2 = ...
end

This does force you to specify what triangle you are working on at any given point in your code, but you can use functors with TRIANGLE arguments to avoid settling on one type of triangle.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!