f# - this construct causes code to be less generic... when applied to phantom type -
i trying implement dsl in f# small language. unfortunately compiler stops me dead in tracks when attempt constrain nodes hold additional type information (via phantom types).
the following lines of code illustrate issue:
type expr<'a> = | int of int | eq of expr<int> * expr<int> | not of expr<bool> let rec to_string (expr: expr<'a>) = match expr | int(n) -> string n | eq(x, y) -> sprintf "%s == %s" (to_string x) (to_string y) | not(b) -> sprintf "!%s" (to_string b)
according compiler construct to_string x
issues following warning:
construct causes code less generic indicated type annotations. type variable 'a has been constrained type 'int'.
following that, on next line, construct to_string b
issues error:
type mismatch. expecting
expr<int>
givenexpr<bool>
typeint
not match typebool
i can't seem find way circumvent behavior, , in fact can't find cause code being less generic had expected. if possible prefer solution doesn't entirely abandon phantom types.
am doing fundamentally wrong? compiler bug?
you need make to_string
function generic:
let rec to_string<'a> (expr:expr<'a>) : string = match expr | int(n) -> string n | eq(x, y) -> sprintf "%s == %s" (to_string x) (to_string y) | not(b) -> sprintf "!%s" (to_string b)
the type parameter inferred when call function
not (eq (int 1, int 2)) |> to_string
Comments
Post a Comment