Pre/Post conditions in OOP

In inheritance, use pre-conditions and post-conditions to enforce invariants ? Subclasses inherit using facilities of programming language. Subtypes satisfy the semantic constraints (Liskov substitution principle).

Issues for initialization

The constructor must establish the invariant. Languages that don’t allow constructors don’t support this principle (not good for OOP). If you can do something like this, in C++:

foo b;

There must be a way to initialize the object implicitly, or to explicitly call constructors. In C++, this calls the default constructor.

Logic Programming

In Prolog:

  • The programmer provides a database of facts and rules of inference
  • Queries formulated as assertions involving facts in database
  • Program execution = attempting to prove an assertion
pred1(...) :- pred2(...), pred3(...),...predN.

A :- B is the same as B implies A. Variables whose first appearance is on the LHS of the clause have implicit universal quantifiers. Those whose first appearance is in the body of the clause have implicit existential quantifiers.

grandmotherOf(X, GM) :- motherOf(M, GM), motherOf(X, M). This reads as “For all X, GM, the grandmother of X is GM if there exists an M such that the mother of M is GM and the mother of X is M”.