开发者

Including two signatures, both with a 'type t' [Standard ML]

A contrived example:

signature A =
sig
  type t
  val x: t
end

signature B =
sig
  type t
  val y: t
end

signature C = sig include A B end

Obviously, this will cause complaints that type t occurs twice in C. But is there any way to express that I want the two ts to be equated, ending up with:

signature C =
sig
  type t
  val x: t
  val y: t
end

I tried all sorts of silly syntax like include B where type t = A.t, which unsurprisingly didn't work. Is there something I've forgotten to try?

Also, I know th开发者_运维技巧at this would be simply answered by checking the language's syntax for anything obvious (or a lack of), but I couldn't find a complete grammar anywhere on the internet.

(FWIW, the actual reason I'm trying to do this is Haskell-style monads and such, where a MonadPlus is just a mix of a Monad and an Alternative; at the moment I'm just repeating the contents of ALTERNATIVE in MONAD_PLUS, which strikes me as less than ideal.)


You're hosed. The best you can do is, as Jordan Lewis suggests, use substructures and a sharing clause. To include two different signatures that both define t is always an error. So mixing ALTERNATIVE and MONAD_PLUS in the way you would like just isn't going to work.

For a proposal of other things that are wrong with include and how to fix them, see An Expressive Language of Signatures.


You're looking for a sharing clause.

signature C =
sig
  structure A1 : A
  structure B1 : B
  sharing type A1.t = B1.t
  type t = A1.t
  val z : t
end

This ensures that A1's t and B1's t are the same, and furthermore uses that same t as the type of a value z.

Standard ML '97's grammar is available here.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜