구현을 전환 할 수있는 다른 구조체를 기반으로 구조체를 만들고 싶습니다.폴리/ML 매개 변수 구조체 값 구성
signature Field =
sig
type field_type;
val zero : field_type;
val add : (field_type * field_type) -> field_type;
val f : int -> field_type;
end;
structure Reals :> Field =
struct
type field_type = real;
val zero = 0.0;
fun add (x,y) = x + y;
fun f x = Real.fromInt(x);
end;
structure Rat :> Field =
struct
type field_type = int * int;
val zero = (0, 1);
fun add ((i1,i2),(j1,j2)) = (i1*j2 + j1*i2, i2*j2);
fun f x = (x, 1);
end;
functor Useable(F : Field) =
struct
type field_type = F.field_type;
val zero = F.zero;
fun add(x,y) = F.add(x,y);
fun f x = F.f x;
end;
structure ReF = Useable(Reals);
structure RaF = Useable(Rat);
아이디어는 필드 (Reals 또는 Rat, 비효율적 인 구현 무시)의 구현을 연결할 수 있다는 것입니다.
는이 코드 ReF.add(ReF.zero, ReF,zero)
를 실행할 수 있습니다,하지만 난 생성자 f: int -> field_types
를 만들어이 문제를 극복하기 위해 ReF.add(0.0, 0.0)
(또는 RaF.add((0,1),(0,1))
)
를 실행할 수 없습니다,하지만 난이 그리 우아하고도 성가신 것으로 판명. ReF.add(0.0, 0.0)
을 (를) 사용할 수 있도록 무엇인가 할 수 있습니까?