我想知道是否有可能在OCaml中构建类似于多个调度的东西。为此,我尝试为多方法的输入签名创建一个显式类型。例如,我定义了一个数字类型
type _ num =
| I : int -> int num
| F : float -> float num
现在我想要一个函数add
来汇总'a num
和'b num
,如果int num
和'a
同时返回'b
是int
,如果其中至少有一个是float num
,则为float
。此外,类型系统应该知道输出将使用哪个构造函数。即它应该在函数调用中静态地知道输出的类型为int num
。
这可能吗?到目前为止,我只能管理签名type a b. a num * b num -> a num
的函数,因此总是必须提供(更一般的)float作为第一个参数。必须禁止使用案例int num * float num
,从而导致非详尽的模式匹配和运行时异常。
似乎需要一个像type a b. a num * b num -> c(a,b) num
这样的签名,其中c
是一个包含类型提升规则的类型函数。我不认为OCaml有这个。打开的类型或对象能够捕获这个吗?我不是在寻找类型之间最常用的函数,只要我能明确列出一些输入类型组合和相应的输出类型就足够了。
答案 0 :(得分:8)
您询问的具体案例可以使用GADT和多态来很好地解决
变种。请参阅此代码底部的M.add
来电:
type whole = [ `Integer ]
type general = [ whole | `Float ]
type _ num =
| I : int -> [> whole ] num
| F : float -> general num
module M :
sig
val add : ([< general ] as 'a) num -> 'a num -> 'a num
val to_int : whole num -> int
val to_float : general num -> float
end =
struct
let add : type a. a num -> a num -> a num = fun a b ->
match a, b with
| I n, I m -> I (n + m)
| F n, I m -> F (n +. float_of_int m)
(* Can't allow the typechecker to see an I pattern first. *)
| _, F m ->
match a with
| I n -> F (float_of_int n +. m)
| F n -> F (n +. m)
let to_int : whole num -> int = fun (I n) -> n
let to_float = function
| I n -> float_of_int n
| F n -> n
end
(* Usage. *)
let () =
M.add (I 1) (I 2) |> M.to_int |> Printf.printf "%i\n";
M.add (I 1) (F 2.) |> M.to_float |> Printf.printf "%f\n";
M.add (F 1.) (I 2) |> M.to_float |> Printf.printf "%f\n";
M.add (F 1.) (F 2.) |> M.to_float |> Printf.printf "%f\n"
打印
3
3.000000
3.000000
3.000000
您无法将上述任何to_float
更改为to_int
:它是静态的
已知仅添加两个I
s会产生I
。但是,你可以改变
to_int
到to_float
(并调整printf
)。这些操作可以轻松地组成和传播类型信息。
具有嵌套match
表达式的foolery是我将要求的hack
邮件列表。我以前从没见过这件事。
AFAIK是评估当前OCaml中常规类型函数的唯一方法 用户提供证人,即一些额外的类型和价值信息。这个 可以通过多种方式完成,例如将参数包装在额外的构造函数中 (请参阅@mookid的回答),使用一流的模块(也在下面讨论 部分),提供一个可供选择的小抽象值列表(其中 实现实际操作,包装器调度到那些值)。该 下面的示例使用第二个GADT来编码有限关系:
type _ num =
| I : int -> int num
| F : float -> float num
(* Witnesses. *)
type (_, _, _) promotion =
| II : (int, int, int) promotion
| IF : (int, float, float) promotion
| FI : (float, int, float) promotion
| FF : (float, float, float) promotion
module M :
sig
val add : ('a, 'b, 'c) promotion -> 'a num -> 'b num -> 'c num
end =
struct
let add (type a) (type b) (type c)
(p : (a, b, c) promotion) (a : a num) (b : b num) : c num =
match p, a, b with
| II, I n, I m -> I (n + m)
| IF, I n, F m -> F (float_of_int n +. m)
| FI, F n, I m -> F (n +. float_of_int m)
| FF, F n, F m -> F (n +. m)
end
(* Usage. *)
let () =
M.add II (I 1) (I 2) |> fun (I n) -> n |> Printf.printf "%i\n";
M.add IF (I 1) (F 2.) |> fun (F n) -> n |> Printf.printf "%f\n"
此处,类型函数为('a, 'b, 'c) promotion
,其中'a
,'b
为'c
参数,add
就是结果。不幸的是,你必须通过promotion
'c
type 'p result = 'c
constraint 'p = (_, _, 'c) promotion
val add : 'a num -> 'b num -> ('a, 'b, _) promotion result num
的{{1}}实例,即这样的东西不会被激活
(AFAIK)的工作:
'c
尽管'a
完全由'b
和val add : 'a num -> 'b num -> 'c num
确定,但由于GADT;编译器仍然认为这基本上只是
add
除了那些,除了那些功能之外,见证人并没有真正为你买四个功能
操作集(multiply
,I
等)和参数/结果类型
组合,可以大部分彼此正交;打字可以
更好,事情可以更容易使用和实施。
编辑它实际上可以放弃F
和val add : ('a, 'b, 'c) promotion -> 'a -> 'b -> `c
构造函数,即
M.add IF 1 2. |> Printf.printf "%f\n"
这使得使用更加简单:
4.02.1+modular-implicits-ber
然而,在这两种情况下,这都不像GADT +多态变体解决方案那样可组合,因为从未推断出见证。
如果您的见证是一流的模块,编译器可以为您选择
自动模块化含义。你可以试试这个代码
module type PROMOTION =
sig
type a
type b
type c
val promotion : (a, b, c) promotion
end
implicit module Promote_int_int =
struct
type a = int
type b = int
type c = int
let promotion = II
end
implicit module Promote_int_float =
struct
type a = int
type b = float
type c = float
let promotion = IF
end
(* Two more like the above. *)
module M' :
sig
val add : {P : PROMOTION} -> P.a num -> P.b num -> P.c num
end =
struct
let add {P : PROMOTION} = M.add P.promotion
end
(* Usage. *)
let () =
M'.add (I 1) (I 2) |> fun (I n) -> n |> Printf.printf "%i\n";
M'.add (I 1) (F 2.) |> fun (F n) -> n |> Printf.printf "%f\n"
切换。第一个示例只是将前一个示例中的GADT见证包装在模块中,以便让编译器为您选择它们:
module type PROMOTING_ADD =
sig
type a
type b
type c
val add : a -> b -> c
end
implicit module Add_int_int =
struct
type a = int
type b = int
type c = int
let add a b = a + b
end
implicit module Add_int_float =
struct
type a = int
type b = float
type c = float
let add a b = (float_of_int a) +. b
end
(* Two more. *)
module M'' :
sig
val add : {P : PROMOTING_ADD} -> P.a -> P.b -> P.c
end =
struct
let add {P : PROMOTING_ADD} = P.add
end
(* Usage. *)
let () =
M''.add 1 2 |> Printf.printf "%i\n";
M''.add 1 2. |> Printf.printf "%f\n"
使用模块化含义,您还可以简单地添加无标记的浮点数和整数。此示例对应于调度函数&#34;见证&#34;:
generator=itertools.combinations_with_replacement('abcd', 4 )
答案 1 :(得分:5)
一种选择是为此使用简单的变体类型,将所有内容包装成一个(可能很大)类型并匹配:
type vnum =
| Int of int
| Float of float
let add_vnum a b =
match a, b with
| Int ia, Int ib -> Int (ia + ib)
| Int i, Float f
| Float f, Int i -> Float (float_of_int i +. f)
| Float fa, Float fb -> Float (fa +. fb)
另一种方法是将输入值限制为具有匹配类型:
type _ gnum =
| I : int -> int gnum
| F : float -> float gnum
let add_gnum (type a) (x : a gnum) (y : a gnum) : a gnum =
match x, y with
| I ia, I ib -> I (ia + ib)
| F fa, F fb -> F (fa +. fb)
最后,其中一个输入值的类型可用于约束返回值的类型。在此示例中,返回值将始终与第二个参数具有相同的类型:
type _ gnum =
| I : int -> int gnum
| F : float -> float gnum
let add_gnum' (type a b) (x : a gnum) (y : b gnum) : b gnum =
match x, y with
| I i1, I i2 -> I (i1 + i2)
| F f1, F f2 -> F (f1 +. f2)
| I i, F f -> F (float_of_int i +. f)
| F f, I i -> I (int_of_float f + i)
答案 2 :(得分:1)
一个选项是使用带有参数元组的子类型,这允许重用一些代码(这就是使用子类型的原因):
type intpair = [`int_int of int * int]
type floatpair = [`float_float of float * float]
type num = [`int of int | `float of float]
type pair =
[ `float_int of float * int
| `int_float of int * float
| intpair | floatpair ]
let plus_int_int = function `int_int (i,j) -> `int (i+j)
let plus_float_float = function `float_float (x,y) -> `float (x+.y)
let plus_int_float = function `int_float (i,y) -> `float(float i +. y)
let plus_float_int = function `float_int (x,j) -> `float(x +. float j)
let plus
: pair -> num
= function
| `int_int _ as a -> plus_int_int a
| `float_float _ as a -> plus_float_float a
| `int_float _ as a -> plus_int_float a
| `float_int _ as a -> plus_float_int a
现在,如果你想要静态保证,你需要使用GADT:
type 'a num =
| Int : int -> int num
| Float : float -> float num
type 'a binop =
| Intpair : (int * int) -> int binop
| Int_Float : (int * float) -> float binop
| Float_Int : (float * int) -> float binop
| Floatpair : (float * float) -> float binop
let plus :
type a . a binop -> a num
= function
| Intpair (a,b) -> Int (a+b)
| Int_Float (a,y) -> Float (float a +. y)
| Float_Int (x,b) -> Float (x +. float b)
| Floatpair (x,y) -> Float (x +. y)