For type theory of object oriented programming, see [1].
There are many models for object oriented programming language concepts. Often type theory of OO is expressed in terms of category theory for modelling functions and abstractions, and domain theory for modelling recursive definitions. Usual models in terms of category theory use final coalgebras [2] [3] to model classes of objects from OO - with recursive types and existentially quantified types used for modelling stateful externally visible behaviour and references to (state of) objects. Methods are typed by signatures. Usually subtyping is preferred to subclassing to focus on interfaces rather than implementations. In such models, subclass coercions that model both downcasts and upcadts between object classes are just functions that preserve the external behaviour, subject to constraints requiring preservation of the hidden contents of existentially quantified "OO object state" (preserve the coalgebra structure and contents of existential packages). Downcasts specifically can be modelled as inverses to an "upcast" projection functions that forget some operations and state - notice the failure possibility from such operation must be carefully modelled, usually in terms of Maybe monad modelling partiality.
Notice though that object oriented programming does not in general have a common theoretical foundation, and many programming language concepts are language specific. Theoretical models usually only address public behaviour of systems consisting of OO objects, and try to abstract out the internal implementation details - i.e. primarily modelling interfaces. The models are sometimes used in discussions on language design and for implementation of compilers for the languages [4]. The theoretical models usually use same foundation that is used for modelling functional programming, to unify the foundational concepts.
A complication that often comes up when discussing theoretical models of object oriented programming is the covariance and contravariance of method signatures with respect to subclassing and subtyping relations. Different parts of the OO object model behave differently with respect to whether subclass and subtype relationships allow generalization or specialization of the corresponding method and function signatures and state, and theoretical models spend lots of time discussing when covariance or contravariance is appropriate. The subclassing relation is very complicated as a consequence, and usual approaches on abstracting that can produce unreadable descriptions involving order relationship between models of class types.
[1] Abadi, Cardelli: "A Theory of Objects", http://lucacardelli.name/indexPast.html
[2] Poll: "Coalgebraic semantics of subtyping"
[3] Crole: "Categories for Types"
[4] Muchnick: Advanced Compiler Design and Implementation"
type-theory
is not the correct tag (suggestions welcome). In terms of programming to an interface, if a function takes aB
, it seems (dare I say) obvious that calling methods thatB
does not have is breaking some kind of rules. I assumed that those rules are type-related, but perhaps that is incorrect terminology. What is the name for the sense in which that is wrong? $\endgroup$if B then P else Q
you want to push the type constraints you get from the conditionB
into the two branches: when type-checkingP
you want to assume the truth of B. ForQ
you want to assume the negation ofB
. Simple typing systems do not allow you to do this, but more sophisticated ones to. Setting up a typing system that lets you do this is a bit non-trivial. $\endgroup$if-then-else
because you become a victim of boolean blindness. A better construct is some sort of a downcast with an optional result. $\endgroup$