In most statically typed languages, each expression has an intrinsic type. E.g. in Java,
3 is an int,
3.0 is a double,
3+3.0 is also a double. Types do not depend on the context of the expression.
However, in the CSS type system described in the spec, some expressions have a type depending on the context. For example, the
red token can be a
<custom-ident>, or an
<attr-name>, depending on the context in which it is used.
I want to build a type system for a CSS-based programming langauge. In this type system, I want to use a typing strategy that does not go from the bottom up (starting from the leaves in the AST), but top down (starting from the root of the AST).
For example, when the compiler needs to derive that the
linear(red, yellow) expression inhabits the
<bg-image> type, it would do the following derivation:
we want to derive that linear(red, yellow) :: <bg-image> <bg-image> is defined as <url> | linear(<color>, <color>), so linear(red, yellow) :: <url> | linear(<color>, <color>) let us try the second branch linear(red, yellow) :: linear(<color>, <color>) let us check sub-expressions red :: <color> yellow :: <color> everything fine, OK
Can you point me to scholarly articles where this kind of typing strategy is discussed?
P.s. I don't know of other languages where a type of the expression depends on the context, except for Perl. In Perl,
("a","b") can be interpreted as either a list or an integer (its length), depending on the context:
print ("a","b") # => ab print 0+("a","b") # => 2