haskell - Conversion between different eDSLs (different type class constraints) -
i'm trying "compile" simple edsl atom language. problem arises here is, type class constraints on types/functions not match of atom language.
one edsl compiles atom copilot, has same problem solves in rather verbose way. follows simplified version of involved datatypes:
{-# language gadts #-} data type tfloat :: type float data op1 b neg :: type -> op1 a class nume instance nume float data exp e eneg :: nume => exp -> exp
type
, op1
part of new edsl, nume
, exp
belong compilation target. convert between edsls @ point need function op2exp
following type:
op2exp :: op1 b -> exp -> exp b
now way atom handles rather verbose:
data numeinst e = nume e => numeinst numeinst :: type -> numeinst numeinst tfloat = numeinst op2exp :: op1 b -> exp -> exp b op2exp op = case op of neg t -> case numeinst t of numeinst -> eneg
this works, quite cumbersome , full of repetition.
question:
is there way, maybing using new language features, write op2exp
function in easier way? ideally along lines of:
op2exp (neg t) = eneg
ideally, wouldn't need type
data type , have compiler figure out types match.
you make op1
datatype parametric in target language, using type parameter of kind * -> constraint
. looking @ atom , ivory libraries, think should work:
{-# language gadts, constraintkinds #-} data op1 expr b neg :: (expr a, num a) => op1 expr a class atomexpr instance atomexpr float data atomexp e eneg :: (atomexpr a, num a) => atomexp -> atomexp op2exp :: op1 atomexpr b -> atomexp -> atomexp b op2exp neg = eneg
Comments
Post a Comment