haskell - Existentials and reusing witness -
let's imagine have existential type t
t = ∃x { :: x, f :: x -> int}
of produce value
v :: t v = pack {int, { = 0, f x = 0 } } t
so :
- users of value forbidden know type
x
used in implementation. in order consume value, code has polymorphic inx
- the implementor, on other hand, in full knowledge
x
int
, can use capacities of underlying type wishes
i know if there variant of mechanism not destroy evidence :
w, v = pack {int, { = 0, f x = 0 } } t s = unpack w v -- recovers type information
where w
value level proof of type equation tying x
int
. idea selectively reuse implementation in part of code, , have non polymorphic code. have usual existential behaviour, can ignore w
returned.
i guess 1 cast x
int
, abandon type safety, that's story: if know secret v
, wouldn't make sense me able tell secret else , have compiler verify secret used code has been given to.
has been tried / what's wrong part of this?
use singletons
-- singleton types interested in data s sint :: s int sbool :: s bool -- existential type, singleton inside data t t :: s -> -> (a -> int) -> t -- producer t :: t t = t sint 3 succ -- consumer foo :: t -> int foo (t sint n f) = f (n + 10) foo (t sbool true f) = 23 foo (t sbool false f) = f 3
if need go full monty, use typeable
.
data t t :: typeable => -> (a -> int) -> t -- consumer foo :: t -> int foo (t x f) = case cast x of n -> f ((n :: int) + 10) nothing -> 12 -- more casts can attempted here
Comments
Post a Comment