lifting into a data type (Haskell) -
type pt_int = int type pt_string = string data polytype = pt_int int | pt_string string given function f, how write function lifts polytype? (just trying understand lifting)
your polytype equivalent either int string. in case haven't seen either before:
data either b = left | right b so have function like
liftp :: (either int string -> a) -> polytype -> liftp f poly = case poly of pt_int -> f (left i) pt_string s -> f (right s) polytype contains either int or string, can lift functions defined on both int , string.
that said, don't think you're after. term "lifting" used in context of polymorphic data types [a], maybe a, (->) a or in general type f a f :: * -> *.
in these cases, given function g :: -> b, want new function [a] -> [b], maybe -> maybe b or in general f -> f b. fmap functor.
class functor f fmap :: (a -> b) -> (f -> f b) but polytype monomorphic (it doesn't have free variable in type. precise, has kind *) can't functor.
you chould change definition of polytype to
data polytype = pt now valid functor (it's identity functor)
instance functor polytype fmap f (pt a) = pt (f a) the type of fmap (specialized particular polytype instance) is
fmap :: (a -> b) -> polytype -> polytype b
Comments
Post a Comment