Why aren't there existentially quantified type variables in GHC Haskell -


there universally quantified type variables, , there existentially quantified data types. however, despite people give pseudocode of form exists a. int -> a explain concepts sometimes, doesn't seem compiler extension there's real interest in. "there isn't value in adding this" kind of thing (because seem valuable me), or there problem undecidability that's makes impossible.

edit: i've marked viorior's answer correct because seems actual reason why not included. i'd add additional commentary though in case want clarify more.

as requested in comments, i'll give example of why consider useful. suppose have data type follows:

data person = person   { age: int   , height: double   , weight: int   , name:   } 

so choose parameterize on a, naming convention (i know makes more sense in example make namingconvention adt appropriate data constructors american "first,middle,last", hispanic "name,paternal name,maternal name", etc. now, go this).

so, there several functions see ignore type person parameterized over. examples be

age :: person -> int height :: person -> double weight :: person -> int 

and function built on top of these ignore a type. example:

atriskfordiabetes :: person -> bool atriskfordiabetes p = age p + weight p > 200 --clearly, not doctor 

now, if have heterogeneous list of people (of type [exists a. person a]), able map of our functions on list. of course, there useless ways map:

heterolist :: [exists a. person a] heterolist = [person 20 30.0 170 "bob jones", person 50 32.0 140 3451115332] extractednames = map name heterolist 

in example, extractednames of course useless because has type [exists a. a]. however, if use our other functions:

totalweight :: [exists a. person a] -> int totalweight = sum . map age  numberatrisk :: [exists a. person a] -> int numberatrisk = length . filter id . map atriskfordiabetes 

now, have useful operates on heterogeneous collection (and, didn't involve typeclasses). notice able reuse our existing functions. using existential data type go follows:

data someperson = forall a. someperson (person a) --fixed, viorior 

but now, how can use age , atriskfordiabetes? can't. think have this:

someage :: someperson -> int someage (someperson p) = age p 

which lame because have rewrite of combinators new type. gets worse if want data type that's parameterized on several type variables. imagine this:

somewhatheteropipelist :: forall b. [exists c d. pipe b c d] 

i won't explain line of thought further, notice you'd rewriting lot of combinators using existential data types.

that being said, hope i've give mildly convincing use useful. if doesn't seem useful (or if example seems contrived), feel free let me know. also, since firstly programmer , have no training in type theory, it's little difficult me see how use skolem's theorum (as posted viorior) here. if show me how apply person a example gave, grateful. thanks.

it unnecessary.

by skolem's theorem convert existential quantifier universal quantifier higher rank types:

(∃b. f(b)) -> int   <===>  ∀b. (f(b) -> int) 

every existentially quantified type of rank n+1 can encoded universally quantified type of rank n


Comments

Popular posts from this blog

c++ - OpenCV Error: Assertion failed <scn == 3 ::scn == 4> in unknown function, -

php - render data via PDO::FETCH_FUNC vs loop -

The canvas has been tainted by cross-origin data in chrome only -