Overlapping Rules and Logic Variables in Functional Logic Programs

Similar documents
Declarative Programming with Function Patterns

Multi-paradigm Declarative Languages

Declarative Multi-Paradigm Programming in

Functional Logic Design Patterns

High-Level Server Side Web Scripting in

Multi-paradigm Declarative Languages

Declarative Multi-paradigm Programming

Functional Logic Programming: From Theory to Curry

CPM: A Declarative Package Manager with Semantic Versioning

Default Rules for Curry

Combining Static and Dynamic Contract Checking for Curry

Functional Logic Programming Language Curry

Evaluation Strategies for Functional Logic Programming

Multi-Paradigm Programming

Adding Constraint Handling Rules to Curry Extended Abstract

INSTITUT FÜR INFORMATIK

Functional Logic Programming. Kristjan Vedel

Concolic Testing of Functional Logic Programs

Functional Logic Programming

Transforming Functional Logic Programs into Monadic Functional Programs

Adding Plural Arguments to Curry Programs

Systematic Generation of Glass-Box Test Cases for Functional Logic Programs

Curry without Success

Implementing Equational Constraints in a Functional Language

Towards a Safe Partial Evaluation of Lazy Functional Logic Programs

Declaring Numbers. Bernd Braßel, Frank Huch and Sebastian Fischer. Department of Computer Science, University of Kiel, Germany

Declarative Programming with Function Patterns

Curry. An Integrated Functional Logic Language. Version January 13, Michael Hanus 1 [editor] Additional Contributors:

CS 457/557: Functional Languages

Dynamic-Cut with Definitional Trees

Evaluation Strategies for Functional Logic Programming

Improving the Efficiency of Non-Deterministic Computations

Programming in Omega Part 1. Tim Sheard Portland State University

Lambda Calculus and Type Inference

Lecture 19: Functions, Types and Data Structures in Haskell

CS 320 Homework One Due 2/5 11:59pm

COMP 4161 NICTA Advanced Course. Advanced Topics in Software Verification. Toby Murray, June Andronick, Gerwin Klein

To figure this out we need a more precise understanding of how ML works

F U N C T I O N A L P E A R L Concurrent Distinct Choices

PROGRAMMING IN HASKELL. CS Chapter 6 - Recursive Functions

λ calculus is inconsistent

Extra Variables Can Be Eliminated from Functional Logic Programs

TEABAG: A DEBUGGER FOR CURRY STEPHEN LEE JOHNSON. A thesis submitted in partial fulfillment of the requirements for the degree of

ISR 2014 Strategies. Hélène KIRCHNER Inria. August Topics, Objectives, Contents. Hélène KIRCHNER Inria ISR 2014 Strategies 1 / 48

Testing. Wouter Swierstra and Alejandro Serrano. Advanced functional programming - Lecture 2. [Faculty of Science Information and Computing Sciences]

c constructor P, Q terms used as propositions G, H hypotheses scope identifier for a notation scope M, module identifiers t, u arbitrary terms

Coq quick reference. Category Example Description. Inductive type with instances defined by constructors, including y of type Y. Inductive X : Univ :=

A Formal Approach to Reasoning about the Effectiveness of Partial Evaluation

Representing Functional Logic Computations

A Semantics for Tracing Declarative Multi-Paradigm Programs

COMP4161: Advanced Topics in Software Verification. fun. Gerwin Klein, June Andronick, Ramana Kumar S2/2016. data61.csiro.au

To figure this out we need a more precise understanding of how ML works

Integration of Finite Domain Constraints in KiCS2

Programming with Math and Logic

Formalization and Abstract Implementation of Rewriting with Nested Rules

A Virtual Machine for Functional Logic Computations

Inductive datatypes in HOL. lessons learned in Formal-Logic Engineering

Haskell 98 in short! CPSC 449 Principles of Programming Languages

CS 242. Fundamentals. Reading: See last slide

Introduction to Programming: Lecture 6

Lambda Calculus and Type Inference

Simple Unification-based Type Inference for GADTs

Inductive Programming

Lambda Calculus and Extensions as Foundation of Functional Programming

3.4 Deduction and Evaluation: Tools Conditional-Equational Logic

Compiling Multi-Paradigm Declarative Programs into Prolog

Theorem Proving Principles, Techniques, Applications Recursion

Lesson 4 Typed Arithmetic Typed Lambda Calculus

Overview. A Compact Introduction to Isabelle/HOL. Tobias Nipkow. System Architecture. Overview of Isabelle/HOL

Exercise 1 ( = 24 points)

Type Processing by Constraint Reasoning

Chapter 5. Pure PROLOG. Foundations of Logic Programming

Zipping Search Trees. Tom Schrijvers. with Denoit Desouter and Bart Demoen

Exercise 1 (2+2+2 points)

A Modular and Generic Analysis Server System for Functional Logic Programs

THE INTEGRATION OF FUNCTIONS INTO LOGIC PROGRAMMING: FROM THEORY TO PRACTICE

Advanced Functional Programming

Functional Programming. Overview. Topics. Definition n-th Fibonacci Number. Graph

Call Pattern Analysis for Functional Logic Programs

Functional Programming in Haskell Part I : Basics

Improving the Efficiency of Non-Deterministic Computations 1

Built-in Module BOOL. Lecture Note 01a

Programming Languages Fall 2014

Shell CSCE 314 TAMU. Functions continued

Concepts of programming languages

Lambda Terms, Types and Tree-Arithmetic

A Semantics for Weakly Encapsulated Search in Functional Logic Programs

The Basic Scheme for the Evaluation of Functional Logic Programs. Arthur Peters

Review. CS152: Programming Languages. Lecture 11 STLC Extensions and Related Topics. Let bindings (CBV) Adding Stuff. Booleans and Conditionals

Introduction to Programming, Aug-Dec 2006

Programming in Haskell Aug-Nov 2015

Functional Programming in Haskell Part 2 : Abstract dataypes and infinite structures

Trees and Inductive Definitions

Symbolic Profiling for Multi-Paradigm Declarative Languages

Extended Static Checking for Haskell (ESC/Haskell)

Haske k ll An introduction to Functional functional programming using Haskell Purely Lazy Example: QuickSort in Java Example: QuickSort in Haskell

Week 5 Tutorial Structural Induction

Programming Language Concepts: Lecture 14

The List Datatype. CSc 372. Comparative Programming Languages. 6 : Haskell Lists. Department of Computer Science University of Arizona

Basic Foundations of Isabelle/HOL

Transcription:

ICLP 2006 Overlapping Rules and Logic Variables in Functional Logic Programs Michael Hanus Christian-Albrechts-Universität Kiel (joint work with Sergio Antoy, Portland State University)

FUNCTIONAL LOGIC LANGUAGES Approach to amalgamate ideas of declarative programming efficient execution principles of functional languages (determinism, laziness) flexibility of logic languages (constraints, built-in search) avoid non-declarative features of Prolog (arithmetic, I/O, cut) combine best of both worlds in a single model (higher-order functions, declarative I/O, concurrent constraints) Advantages: optimal evaluation strategies [JACM 00,ALP 97] new design patterns [FLOPS 02] better abstractions for application programming (GUI programming [PADL 00], web programming [PADL 01, PPDP 06]) CAU Kiel Michael Hanus 2

FUNCTIONAL LOGIC LANGUAGES Approach to amalgamate ideas of declarative programming efficient execution principles of functional languages (determinism, laziness) flexibility of logic languages (constraints, built-in search) avoid non-declarative features of Prolog (arithmetic, I/O, cut) combine best of both worlds in a single model (higher-order functions, declarative I/O, concurrent constraints) Advantages: optimal evaluation strategies [JACM 00,ALP 97] new design patterns [FLOPS 02] better abstractions for application programming (GUI programming [PADL 00], web programming [PADL 01, PPDP 06]) Minimal kernel language for FLP? CAU Kiel Michael Hanus 2

FUNCTIONAL LANGUAGES (E.G., HASKELL) CAU Kiel Michael Hanus 3

FUNCTIONAL LANGUAGES (E.G., HASKELL) [] ++ ys = ys (x:xs) ++ ys = x : xs ++ ys CAU Kiel Michael Hanus 3

FUNCTIONAL LANGUAGES (E.G., HASKELL) + logic variables (expressive power) [] ++ ys = ys (x:xs) ++ ys = x : xs ++ ys last xs ys ++ [x] =:= xs = x where x,ys free CAU Kiel Michael Hanus 3

FUNCTIONAL LANGUAGES (E.G., HASKELL) + logic variables (expressive power) [] ++ ys = ys (x:xs) ++ ys = x : xs ++ ys last xs ys ++ [x] =:= xs = x where x,ys free + overlapping rules (demand-driven search) insert e [] = [e] insert e (x:xs) = e : x : xs insert e (x:xs) = x : insert e xs -- overlapping -- rules CAU Kiel Michael Hanus 3

FUNCTIONAL LANGUAGES (E.G., HASKELL) + logic variables (expressive power) [] ++ ys = ys (x:xs) ++ ys = x : xs ++ ys last xs ys ++ [x] =:= xs = x where x,ys free + overlapping rules (demand-driven search) insert e [] = [e] insert e (x:xs) = e : x : xs insert e (x:xs) = x : insert e xs -- overlapping -- rules perm [] = [] perm (x:xs) = insert x (perm xs) perm [1,2,3] [1,2,3] [1,3,2] [2,1,3]... = functional logic languages Main result of this paper: only one of these extensions is sufficient! CAU Kiel Michael Hanus 3

TERM REWRITING SYSTEMS (TRS) Formal model for functional logic languages: CAU Kiel Michael Hanus 4

TERM REWRITING SYSTEMS (TRS) Formal model for functional logic languages: Datatypes ( admissible values): enumerate all data constructors data Bool = True False data Nat = O S Nat data List = [] Nat : List CAU Kiel Michael Hanus 4

TERM REWRITING SYSTEMS (TRS) Formal model for functional logic languages: Datatypes ( admissible values): enumerate all data constructors data Bool = True False data Nat = O S Nat data List = [] Nat : List Rewrite rules: define operations on values f(t 1,..., t n ) r defined operation patterns (linear data terms) expression add(o,y) y positive(o) False add(s(x),y) S(add(x,y)) positive(s(x)) True Extra variable: rule variable not occurring in left-hand side CAU Kiel Michael Hanus 4

CLASSES OF TERM REWRITING SYSTEMS CAU Kiel Michael Hanus 5

CLASSES OF TERM REWRITING SYSTEMS Inductively sequential term rewriting systems: no overlapping left-hand sides in rules operations inductively defined on datatypes leq(o,x) True cond(true,x) x leq(s(x),o) False leq(s(x),s(y)) leq(x,y) seven S(S(S(S(S(S(S(O))))))) CAU Kiel Michael Hanus 5

CLASSES OF TERM REWRITING SYSTEMS Inductively sequential term rewriting systems: no overlapping left-hand sides in rules operations inductively defined on datatypes leq(o,x) True cond(true,x) x leq(s(x),o) False leq(s(x),s(y)) leq(x,y) seven S(S(S(S(S(S(S(O))))))) ISX: inductively sequential TRS with extra variables smallnum cond(leq(x,seven),x) CAU Kiel Michael Hanus 5

CLASSES OF TERM REWRITING SYSTEMS Inductively sequential term rewriting systems: no overlapping left-hand sides in rules operations inductively defined on datatypes leq(o,x) True cond(true,x) x leq(s(x),o) False leq(s(x),s(y)) leq(x,y) seven S(S(S(S(S(S(S(O))))))) ISX: inductively sequential TRS with extra variables smallnum cond(leq(x,seven),x) OIS: overlapping inductively sequential TRS: allow rules with multiple right-hand sides: l r 1?? r k parent(x) mother(x)? father(x) CAU Kiel Michael Hanus 5

CLASSES OF TERM REWRITING SYSTEMS Inductively sequential term rewriting systems: no overlapping left-hand sides in rules operations inductively defined on datatypes leq(o,x) True cond(true,x) x leq(s(x),o) False leq(s(x),s(y)) leq(x,y) seven S(S(S(S(S(S(S(O))))))) ISX: inductively sequential TRS with extra variables smallnum cond(leq(x,seven),x) OIS: overlapping inductively sequential TRS: allow rules with multiple right-hand sides: l r 1?? r k parent(x) mother(x)? father(x) Main result: OIS with rewriting ISX with narrowing CAU Kiel Michael Hanus 5

EVALUATION: REWRITING VS. NARROWING Functional evaluation: (lazy) rewriting add(s(o),s(o)) S(add(O,S(O))) S(S(O)) CAU Kiel Michael Hanus 6

EVALUATION: REWRITING VS. NARROWING Functional evaluation: (lazy) rewriting add(s(o),s(o)) S(add(O,S(O))) S(S(O)) Functional logic evaluation: narrowing: guess values + rewriting add(x,s(o)) =:= S(S(S(O))) {x S(S(O))} CAU Kiel Michael Hanus 6

EVALUATION: REWRITING VS. NARROWING Functional evaluation: (lazy) rewriting add(s(o),s(o)) S(add(O,S(O))) S(S(O)) Functional logic evaluation: narrowing: guess values + rewriting add(x,s(o)) =:= S(S(S(O))) {x S(S(O))} Needed narrowing: demand-driven variable instantiation and rewriting leq(x,add(o,s(o))) {x O} True Sound, complete, optimal evaluation strategy [JACM 00] CAU Kiel Michael Hanus 6

ELIMINATING OVERLAPPING RULES Transformation OE: OIS ISX: Replace overlapping rule f(t n ) r 1?? r k by: f(t n ) f (y, x l ) (where Var(t n ) = {x 1,..., x l }, y fresh, f new) f (I 1, x l ) r 1. f (I k, x l ) r k data Ix = I 1 I k (index type, e.g., natural numbers) CAU Kiel Michael Hanus 7

ELIMINATING OVERLAPPING RULES Transformation OE: OIS ISX: Replace overlapping rule f(t n ) r 1?? r k by: f(t n ) f (y, x l ) (where Var(t n ) = {x 1,..., x l }, y fresh, f new) f (I 1, x l ) r 1. f (I k, x l ) r k data Ix = I 1 I k (index type, e.g., natural numbers) Example: parent(x) mother(x)? father(x) OE data Iparent = I0 I1 parent(x) parent (y,x) parent (IO,x) mother(x) parent (I1,x) father(x) CAU Kiel Michael Hanus 7

ELIMINATING OVERLAPPING RULES: RESULTS Proposition: R OIS OE(R) ISX CAU Kiel Michael Hanus 8

ELIMINATING OVERLAPPING RULES: RESULTS Proposition: R OIS OE(R) ISX Correctness w.r.t. results computed by needed narrowing: Theorem: R OIS, R = OE(R), t, s terms of R: Soundness: t NN * σ s w.r.t. R t NN * σ s w.r.t. R with σ = Var(t) σ Completeness: t NN * σ s w.r.t. R t NN * σ s w.r.t. R with σ = Var(t) σ CAU Kiel Michael Hanus 8

ELIMINATING OVERLAPPING RULES: RESULTS Proposition: R OIS OE(R) ISX Correctness w.r.t. results computed by needed narrowing: Theorem: R OIS, R = OE(R), t, s terms of R: Soundness: t NN * σ s w.r.t. R t NN * σ s w.r.t. R with σ = Var(t) σ Completeness: t NN * σ s w.r.t. R t NN * σ s w.r.t. R with σ = Var(t) σ Implementations need not implement overlapping rules (done in several implementations but without formal justification) CAU Kiel Michael Hanus 8

ELIMINATING LOGIC VARIABLES Transformation XE: ISX OIS (extra variable elimination) OIS : overlapping inductively sequential without extra variables Evaluation in OIS : rewriting (not narrowing) Thus: programs transformed by XE implementation without handling of logic variables and substitutions CAU Kiel Michael Hanus 9

ELIMINATING LOGIC VARIABLES Transformation XE: ISX OIS (extra variable elimination) OIS : overlapping inductively sequential without extra variables Evaluation in OIS : rewriting (not narrowing) Thus: programs transformed by XE implementation without handling of logic variables and substitutions Basic idea of XE: replace extra variables in rules by value generators CAU Kiel Michael Hanus 9

VALUE GENERATORS S: sort defined by datatype declaration data S = C 1 t 11... t 1a1... C n t n1... t nan Value generator operation instofs defined by (overlapping) rules instofs C 1 (instoft 11,...,instOft 1a1 )?...? C n (instoft n1,...,instoft nan ) CAU Kiel Michael Hanus 10

VALUE GENERATORS S: sort defined by datatype declaration data S = C 1 t 11... t 1a1... C n t n1... t nan Value generator operation instofs defined by (overlapping) rules instofs C 1 (instoft 11,...,instOft 1a1 )?...? C n (instoft n1,...,instoft nan ) Example: data TreeNat = Leaf Branch Nat TreeNat TreeNat instoftreenat Leaf? Branch(instOfNat,instOfTreeNat,instOfTreeNat) instoftreenat O? S(instOfNat) CAU Kiel Michael Hanus 10

VALUE GENERATORS S: sort defined by datatype declaration data S = C 1 t 11... t 1a1... C n t n1... t nan Value generator operation instofs defined by (overlapping) rules instofs C 1 (instoft 11,...,instOft 1a1 )?...? C n (instoft n1,...,instoft nan ) Example: data TreeNat = Leaf Branch Nat TreeNat TreeNat instoftreenat Leaf? Branch(instOfNat,instOfTreeNat,instOfTreeNat) instoftreenat O? S(instOfNat) Lemma: ground constructor terms t of sort S: instofs t CAU Kiel Michael Hanus 10

EXTRA VARIABLE ELIMINATION Transformation XE: OIS OIS replace each extra variable v of sort S in a rule by instofs CAU Kiel Michael Hanus 11

EXTRA VARIABLE ELIMINATION Transformation XE: OIS OIS replace each extra variable v of sort S in a rule by instofs Example: even add(x,x) XE even add(instofnat,instofnat) CAU Kiel Michael Hanus 11

EXTRA VARIABLE ELIMINATION Transformation XE: OIS OIS replace each extra variable v of sort S in a rule by instofs Example: even add(x,x) XE even add(instofnat,instofnat) Lemma (Completeness of XE): t u w.r.t. R XE(t) v w.r.t. XE(R) (v: ground constructor instance of u) CAU Kiel Michael Hanus 11

EXTRA VARIABLE ELIMINATION Transformation XE: OIS OIS replace each extra variable v of sort S in a rule by instofs Example: even add(x,x) XE even add(instofnat,instofnat) Lemma (Completeness of XE): t u w.r.t. R XE(t) v w.r.t. XE(R) (v: ground constructor instance of u) XE is not sound: even add(instofnat,instofnat) + add(o,s(o)) S(O) CAU Kiel Michael Hanus 11

EXTRA VARIABLE ELIMINATION Transformation XE: OIS OIS replace each extra variable v of sort S in a rule by instofs Example: even add(x,x) XE even add(instofnat,instofnat) Lemma (Completeness of XE): t u w.r.t. R XE(t) v w.r.t. XE(R) (v: ground constructor instance of u) XE is not sound: even add(instofnat,instofnat) + add(o,s(o)) S(O) Solution: identical reductions for extra variable generators CAU Kiel Michael Hanus 11

ADMISSIBLE DERIVATIONS Admissible derivation: apply to occurrences of instof (originating from same extra variable) identical reduction steps CAU Kiel Michael Hanus 12

ADMISSIBLE DERIVATIONS Admissible derivation: apply to occurrences of instof (originating from same extra variable) identical reduction steps Implementation: use let bindings available in many languages Example: even add(x,x) XEP even let x = instofnat in add(x,x) CAU Kiel Michael Hanus 12

ADMISSIBLE DERIVATIONS Admissible derivation: apply to occurrences of instof (originating from same extra variable) identical reduction steps Implementation: use let bindings available in many languages Example: even add(x,x) XEP even let x = instofnat in add(x,x) Formalization (details in paper) by transformation XEP : term/substitution pairs where substitution contains instof operations CAU Kiel Michael Hanus 12

ADMISSIBLE DERIVATIONS Admissible derivation: apply to occurrences of instof (originating from same extra variable) identical reduction steps Implementation: use let bindings available in many languages Example: even add(x,x) XEP even let x = instofnat in add(x,x) Formalization (details in paper) by transformation XEP : term/substitution pairs where substitution contains instof operations Theorem: transformation XEP is sound and complete CAU Kiel Michael Hanus 12

ELIMINATING EXTRA VARIABLES: INITIAL GOALS Transformation XEP shows: logic variables can be eliminated if overlapping rules are supported CAU Kiel Michael Hanus 13

ELIMINATING EXTRA VARIABLES: INITIAL GOALS Transformation XEP shows: logic variables can be eliminated if overlapping rules are supported Initial goals with logic variables: t with Var(t) = {x 1,..., x n } start computation with initial term (t, x 1,..., x n ): Result (e, b 1,..., b n ): e computed value b 1,..., b n computed answer CAU Kiel Michael Hanus 13

CONCLUSIONS Two transformations on functional logic programs: 1. eliminate overlapping rules by extra variables any functional logic program can be mapped into ISX ISX core language for functional logic programming practice: ISX already used as core in some implementations 2. eliminate extra variables by new operations (overlapping rules) correctness requires admissible derivations implement admissible derivations by sharing / let expressions CAU Kiel Michael Hanus 14

CONCLUSIONS Two transformations on functional logic programs: 1. eliminate overlapping rules by extra variables any functional logic program can be mapped into ISX ISX core language for functional logic programming practice: ISX already used as core in some implementations 2. eliminate extra variables by new operations (overlapping rules) correctness requires admissible derivations implement admissible derivations by sharing / let expressions Results useful for better understanding of functional logic programming features simpler core languages (support overlapping rules or extra variables) simpler implementations simplify tool support (e.g., current tracers, profilers, slicers, partial evaluators: core language with overlapping rules and extra variables) better understanding of the role of logic variables CAU Kiel Michael Hanus 14