0

I have been trying to build a type system with the Hindley-Milner Algorithm and ran into the following challenge and was curious if there are any resources or papers out there I can take a look at.

Suppose I have a programming language that has some form of property accessor (javascript-like) that works for both arrays and objects, s.t. the property within the brackets for an array needs to be a number and the property within the brackets for an object needs to be a string.

For example

const arr = [0, 1, 2]
arr[0]

const obj = { hello: "World" }
obj["hello"]

Suppose we want to use hindley-milner on the following code snippet

A[B]

Then if later we realized that B is a number, then we instantly deduce that A is an array. Similarly, if we deduce that A is an object, then B is instantly a number.

Are there any papers or type systems out there that have this sort of conditional substitution concept?

Brian Shih
  • 301
  • 1
  • 8
  • Just treat the property access like an overloaded function call. There should be plenty of papers on polymorphism. – Bergi Jan 17 '21 at 02:58
  • Parametricity demands that `B` can be of any type, but you restrict it to `Number|String`. This doesn't work. You need adhoc polymorphism as Bergi suggested. –  Jan 17 '21 at 10:56

0 Answers0