CS 541 Lecture -*- Outline -*- * Semantics of Equational Logic Programming Languages ** Operational Semantics is term rewriting *** Some definitions **** ::= | | ( ... ) ground term has no variables **** least sort of term determined inductively (Reynolds): sort of varId or OpName is given in signature (must be unique) least sort of OpName applied to subterms each subterm has a least sort there must be some decl of OpName with at least those arg sorts by regularity, there is a least such decl of the operator its value sort is the sort of the whole term **** substitution map from variables to terms, such that variables in domain do not occur in range. what prolog prints out (part of the substitution). e.g., s: {X, Y} -> {algol, father(me,dad)} such that s[[X]] = algol, s[[Y]] = father(me,dad). empty: {} -> {} counterexample: bad: {X} -> {X} **** instance of a term result of applying a substitution whose domain includes variables in the term e.g., father(me,dad) is an instance of father(X,dad). where X is a variable. **** unifier: a subsitution that makes two terms identical. determines a common instance (by applying the substitution). % star(X) unifies with star(algol) % by substituion s(X)=algol efficiency: computation unifier linear in size of the smallest term being compared *** Recursive process for executing reduce in MODULE : E . (unlike Prolog, there are no variables in expression E) 1. find a subexpression of E that unifies with the lhs of some equation in MODULE; *if no such subexpression, then done*. Let the unifying substitution be theta. i.e. (theta(lhs) = subexpression) 2. if the equation found was conditional, then evaluate theta(condition); if theta(condition) reduces to true, continue, otherwise go back to step 1 and look for another equation. 3. reduce in MODULE : E[theta(rhs)/subexpression] . where rhs is the right-hand-side of above rule. **** Normal forms if system of equations is confluent (Church-Rosser) (order doesn't matter) and terminating (no infinite reduction sequences) then can reduce each term to a normal form. (call rule set cannonical) rules that define total computable operations over total computable sets can always be chosen so that they are Church-Rosser and terminating. (the usual case for ADTs) built-in operation == (infix) can be applied to test whether two terms have same normal form. terms in normal form can be thought of as set of objects of that sort (carrier set). interpreter reduces to normal form (if it can) **** Bookkeeping need bookkeeping to find a conditional match that works (and not retry) associative/commutative matching requires lots of bookkeeping evauating conditions can require arbitrary computation. **** Strategies many places in a term to try looking for replacements default: all argument places that contain a non-variable term in *some rule* are evaluated to normal form before applying rules to the whole term. explicit spec. of strategies (give user control): op _+_ : Int Int -> Int [strat (1 2 0)] . evalute args 1 and two first, then apply the rules at the top. lazy (normal order): op cons : Sexp Sexp -> Sexp [strat (0)] . don't evaluate the arguments in this context op cons : Sexp Sexp -> Sexp [strat (-1 -2)] . reduce only on demand (when used from top level for printing). ** Denotational Semantics recall equational calculus from earlier lecture. *** The denotation of a program fits directly into the underlying logic (don't need complex stuff like denotational semantics) true of Pure Prolog just as much as "pure" OBJ (without strategies) **** Models programs (logical sentences) describe mathematical objects e.g., sets, algebraic structures with functions, ... e.g., the axioms of Bool have boolean algebras as models e.g., M_{true} = \x.\y.x M_{false} = \x.\y.y M_{or} = \a.\b.if a = true then true else b ***** interpretation of a sentence in a model (semantics) resursive def: eval(OpName) = M_{OpName} eval(f(x,y)) = M_f(eval(x), eval(y)) e.g., eval(true) = M_{true} eval(or(true,false)) = M_{or}(eval(true),eval(false)) ***** semantics of a specification (set of axioms) is a set of models such that each instance of the axioms are valid. possibly some other conditions (initiality) **** validity (M |= E) defines when a ground sentence is valid in some model for normal logic, eval(E) = true for ground equations, if eval(lhs) = eval(rhs). say E is valid in M **** proof or entailment (S |- E) defines when E follows from specification (set of axioms) S the normal concept of a logic, e.g., equational logic -syntactic notion of proof -like ==> for lambda calculus or operational semantics if S |- E say that E is provable from S **** Theory collection of provable sentences. want (soundness of logic) S |- E implies for all models M |= E (completeness of logic) for all M, M |= E implies S |- E *** For OBJ **** model of an obj module is an algebra algebra = carrier set (for each sort) + operations (sorted) semantics of equational specification is a (set of) algebras in OBJ, semantics of theory module is all algebras semantics of object module is initial algebra initial algebra is the term algebra modulo the equations **** logic is equational logic (from before) this logic is sound and complete! **** Correctness of OBJ implementation if S is confluent and terminating, then order doesn't matter so denotation of object module is the intial model problems: term rewriting strategies (partiallity)