290 likes | 462 Views
Gilmore Logic as a Basis for Programming Languages. Jamie Andrews Dept. of Computer Science Univ. of Western Ontario Visiting SITE Sep-Dec 2003. Styles of Programming Languages. Imperative: tell computer what to do Examples: Fortran, C, Java Functional: define functions
E N D
Gilmore Logic as a Basis for Programming Languages Jamie Andrews Dept. of Computer Science Univ. of Western Ontario Visiting SITE Sep-Dec 2003
Styles of Programming Languages • Imperative: tell computer what to do • Examples: Fortran, C, Java • Functional: define functions • Examples: Lisp, ML, Haskell • Logic: define relations • Examples: Prolog, Mercury • Object-Oriented: objects contain both data and functions • Examples: Smalltalk, C++, Java
Functions Are Relations • A function f:(S -> T) can be seen as a set of pairs {(x,y) | f(x) = y} • A relation r on (S x T) can be seen as a set of pairs {(x,y) | r(x,y)} • Thus a function is a relation • Thus functional programming can be based on logic (relational) programming • Right?
Benefits • All styles of programming languages given semantics wrt base logic programming language • Logical specifications -> proofs of properties of base logic programs • No need to define logics for individual programming languages • Connection to field of logic
The Problem • Functional languages use rich type systems • Rich type systems needed to describe (e.g.) object-oriented features • Rich type systems cause inconsistencies in most higher order logics
Example: Universal Types • Functional programs use lists a lot • Need lists of integers, lists of strings, etc. • Want nil (the empty list) to be of type (forall (T:type) (list T)) • i.e. for all types T, nil is of type (list T)
Example: Fixpoint Types • Say we want employee objects to be of type Employee • Want to be able to say • Employee e1; • Employee e2; • … • if (e1.equals(e2)) {…}
Fixpoint Types cont’d • Thus we want • Type Employee defined-as {… equals:(Employee -> boolean) …} • Employee type to be self-referential • To be precise, want • Type Employee defined-as fixpoint T{… equals:(T -> boolean) …}
Higher Order Logic • Phrase used to refer to logic with arbitrary lambda-abstraction and quantification • Needed if we want to interpret modern functional programming languages in logic
The Y Combinator • Y is a term such that (YM) = M(YM) for all terms M • Useful for describing recursive functions • Can appear in untyped lambda calculi • Y causes inconsistencies in most higher order logics • Y is typable in any type system with universal and fixpoint types (forall (T:type) ((T -> T) -> T)) • Oops!
Curry’s Paradox • One reason why Y is not in most HOL • Works in any logic with l, Y and => • Let M be (lx.(x => A)) for arbitrary A • Then (YM) = (M(YM)) = ((YM) => A) • Assume (YM) |- (YM) • Thus (YM) |- ((YM) => A) • So by modus ponens (YM) |- A, and so 2 things: • (1) ((YM) => A) |- A • (2) |- ((YM) => A)) • So from (1) and (2) we have…. |- A
Other Solutions • Scrap the vision • Don’t base programming languages on logic • Restrict programming languages to use type systems consistent with HOL • Confuses two uses of type systems • Restricts expressivity of programming languages
Gilmore Logic • Most higher order logics gain consistency from type systems • Russell, Church, Curry, Scott, Milner, Plotkin, Coquand…….. • Gilmore logics gain consistency by distinguishing between use and mention
Terms of Gilmore Logic • Constants, predicate names and variables • Lambda-abstractions and applications • Propositional connectives • Quantifiers • See TPHOLs 2002 work in progress track paper
Use and Mention • Two flavours of variables: use and mention • Use variable ~= property • Mention variable ~= name of a property • Mention term is any term without a free use variable • Use term can be: • Predicate name • Use variable • Use term applied to mention term
Examples of Use Terms • p • X where X is a use variable • (p a) • (X a) • (p a q) • (X a b y p q) where y is a mention variable • But not (X a b Y) • And not (p X)
Examples of Mention Terms • a • p • (p a) • (p q r y) • ly.lw.(forall X ((y X) => (X w))) • Let’s call this term “inter” • Describes intersection of sets with property y
A Use for inter • Let z be the term lu.((u 0) & forall x ((u x) => (u (s x))) • Property of being a zero-successor set • Thus (inter z) is • The intersection of all zero-successor sets • The set of natural numbers • Let’s call it n
Rules of Gilmore Logic • Propositional and quantifier rules from standard sequent calculus (e.g. Gentzen’s LK) • Witness term flavours must agree with quantified var • Lambda rules: e.g. • Axiom: where N =ab M • But only for use terms M, N G, M[x/N] |- D ----------------------- G, ((lx.M) N) |- D ----------------------- G, M |- D, N
Effect of Definitions and Rules • Remember n? (intersection of all zero-successor sets) • Proving |- forall x ((n x) => (M x)) reduces to proving two sequents: • |- (M 0) • (M x) |- (M (s x)) • Built-in induction
Consistency • Consistent model theory exists • Model assigns: • Mention terms to mention variables • Relations on mention terms to predicates and use variables • Model M |= A if either • A is a use term true under assignment of truth • A is built up from use terms by connectives • Cut-elimination may be possible
The Y Combinator • Curry’s paradox does not bite • (Y M) is fine as a term • However it is not a use term • Therefore (Y M) |- (Y M) not an axiom • Recursive sets (relations) can be defined using Y as well • (Y (ln.lx.(x=0 \/ exists y (x=(s y) & (n y))))) • But with this, cannot prove properties by induction
Functions to Relations • Function definition, e.g. (f x) defined-as (g x (h x)) can be seen as relation definition (fr x y) defined-as exists z ((hr x z) & (gr x z y)) • Strict/lazy distinction
Procedures to Relations • Procedure definition, e.g. p(x) defined-as x := x+2; x := x*3 can be seen as relation definition (pr x y) defined-as exists z (z = x+2 & y = z*3)
Gilmore Logic and Types • Use/mention distinction orthogonal to types • Different type systems can be layered on top of Gilmore logic • Restrict terms and formulas considered • e.g. with universal and fixpoint types
Questions • What is the best way to define recursive relations? • Are there theories that can be defined in (e.g.) Coq that cannot be defined in Gilmore logic? • Vice versa? • Relationship between intersection and Y combinator form of recursive relations?
Conclusions • Gilmore logic offers promising base logic on which to build programming languages • Weak inherent type system => expressivity • Use variables => possibility of proving theorems • Many questions for further study