Evaluation Strategies for Functional Logic Programming

Similar documents
Functional Logic Programming: From Theory to Curry

Multi-paradigm Declarative Languages

Evaluation Strategies for Functional Logic Programming

Multi-paradigm Declarative Languages

Multi-Paradigm Programming

Overlapping Rules and Logic Variables in Functional Logic Programs

Default Rules for Curry

Rewriting. Andreas Rümpel Faculty of Computer Science Technische Universität Dresden Dresden, Germany.

Formalization and Abstract Implementation of Rewriting with Nested Rules

A Formal Approach to Reasoning about the Effectiveness of Partial Evaluation

Declarative Multi-Paradigm Programming in

Functional Logic Design Patterns

Data-Driven Induction of Recursive Functions from Input/Output-Examples

3.4 Deduction and Evaluation: Tools Conditional-Equational Logic

Functional Logic Programming. Kristjan Vedel

CMSC 336: Type Systems for Programming Languages Lecture 5: Simply Typed Lambda Calculus Acar & Ahmed January 24, 2008

Compiling a Functional Logic Language: The Fair Scheme

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

Declarative Multi-paradigm Programming

J. Barkley Rosser, 81, a professor emeritus of mathematics and computer science at the University of Wisconsin who had served in government, died

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

Functional Logic Programming

Improving the Efficiency of Non-Deterministic Computations

1 Scope, Bound and Free Occurrences, Closed Terms

Functional Logic Programming Language Curry

Uncurrying for Termination

Declarative Programming with Function Patterns

Higher-Order Recursive Path Orderings à la carte

Conjunctive queries. Many computational problems are much easier for conjunctive queries than for general first-order queries.

Functional Programming. Overview. Topics. Recall λ-terms. Examples

Concepts of programming languages

Curry without Success

For several classes of TRSs, innermost normalisation already suces for termination [Gra95, Gra96]. Moreover, several modularity results exist for inne

Interaction Nets vs. the ρ-calculus: Introducing Bigraphical Nets

Induction in Coq. Nate Foster Spring 2018

Lecture Notes on Computational Interpretations of Modalities

Inductive Programming

6.001 Notes: Section 6.1

Modal Logic: Implications for Design of a Language for Distributed Computation p.1/53

On Meaning Preservation of a Calculus of Records

A Virtual Machine for Functional Logic Computations

Lesson 4 Typed Arithmetic Typed Lambda Calculus

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

MIT Specifying Languages with Regular Expressions and Context-Free Grammars

MIT Specifying Languages with Regular Expressions and Context-Free Grammars. Martin Rinard Massachusetts Institute of Technology

The Metalanguage λprolog and Its Implementation

Computing Fundamentals 2 Introduction to CafeOBJ

Fundamental Concepts. Chapter 1

High-Level Server Side Web Scripting in

Constrained Types and their Expressiveness

Cover Page. The handle holds various files of this Leiden University dissertation

Journal of Symbolic Computation

Towards a Safe Partial Evaluation of Lazy Functional Logic Programs

3.7 Denotational Semantics

Denitional Trees. Sergio Antoy? Portland State University. but not the control. We introduce a hierarchical structure representing, at a

Chapter 2 The Language PCF

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

Chapter 8: Enhanced ER Model

Improving the Efficiency of Non-Deterministic Computations 1

Lambda Calculus. Type Systems, Lectures 3. Jevgeni Kabanov Tartu,

Derivation of a Pattern-Matching Compiler

CS152: Programming Languages. Lecture 11 STLC Extensions and Related Topics. Dan Grossman Spring 2011

Parallel Programming Patterns Overview CS 472 Concurrent & Parallel Programming University of Evansville

Meta-programming with Names and Necessity p.1

Greedy regular expression matching

CS293S Redundancy Removal. Yufei Ding

CITS3211 FUNCTIONAL PROGRAMMING. 14. Graph reduction

4.5 Pure Linear Functional Programming

From the λ-calculus to Functional Programming Drew McDermott Posted

Type Checking and Type Equality

Types. Type checking. Why Do We Need Type Systems? Types and Operations. What is a type? Consensus

Parsing Expression Grammar and Packrat Parsing

Evaluation Trees for Proposition Algebra

CSE450 Translation of Programming Languages. Lecture 4: Syntax Analysis

Substitution in Structural Operational Semantics and value-passing process calculi

process variable x,y,a,b,c: integer begin x := b; -- d2 -- while (x < c) loop end loop; end process; d: a := b + c

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

Functional Logic Overloading

RIGOROUSLY AUTOMATING TRANSFORMATIONS OF UML BEHAVIOR MODELS

Foundations of AI. 9. Predicate Logic. Syntax and Semantics, Normal Forms, Herbrand Expansion, Resolution

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

Representing Functional Logic Computations

Practice Problems for the Final

The Worker/Wrapper Transformation

An Implementation of the Language Lambda Prolog Organized around Higher-Order Pattern Unification

Matching Logic A New Program Verification Approach

Extending the Interaction Nets Calculus by Generic Rules

Once Upon a Polymorphic Type

Verifying Program Invariants with Refinement Types

Faculty of Electrical Engineering, Mathematics, and Computer Science Delft University of Technology

Data-Driven Induction of Recursive Functions from Input/Output-Examples

Lambda Calculus and Type Inference

IN5050: Programming heterogeneous multi-core processors Thinking Parallel

Lecture 5: Declarative Programming. The Declarative Kernel Language Machine. September 12th, 2011

4 Fractional Dimension of Posets from Trees

Trees. 3. (Minimally Connected) G is connected and deleting any of its edges gives rise to a disconnected graph.

correlated to the Michigan High School Mathematics Content Expectations

The Chemical Abstract Machine

6. Hoare Logic and Weakest Preconditions

Rewriting Needs Constraints and Constraints Need Rewriting

Transcription:

Evaluation Strategies for Functional Logic Programming WRS 01, Utrecht, May 26 Sergio Antoy antoy@cs.pdx.edu Portland State University 1

Outline Whats, hows and whys of Functional Logic Programming. Constructor-based rewrite systems as programs. The role of strategies. The classes of rewrite systems. A strategy for each class. 2

What is FLP FLP studies programming languages that integrate functional and logic programming. A program in these languages is a constructor-based, possibly conditional, rewrite system. A computation is a rewrite or narrowing derivation of a term to a constructor term. A strategy is the most critical component of a FLP language. 3

FLP Features Logic variables (partial structures) Inversion of computations Non-determinism Infinite structures Functional nesting Strategies 4

FLP Advantages Expressiveness (compute more with less code) Transparency (code is not cryptic/opaque) Economy (save code) Improved entire software cycle 5

Example N-queens puzzle queens X -> Y :- Y=permute X, void (capture Y) permute [] -> [] permute [X X s ] -> U++[X]++V :- U++V=permute X s capture Y :- -++[Y 1 ]++K++[Y 2 ]++-=Y, abs(y 1 -Y 2 )=length K+1 This program is (for the most part) a constructor-based conditional rewrite system (with several liberties concerning the traditional notation). queens [1,2,3,4] evaluates (among others) to [2,4,1,3] 6

Execution The execution of an FLP program is by narrowing and/or residuation. Ground (sub)terms are simply rewritten. Non-ground (sub)terms are either instantiated enough to be rewritten or suspended until they become instantiated enough. A strategy schedules the (sub)terms to evaluate, decides whether to suspend or to instantiate and if the latter, computes the instantiation. Different classes of rewrite systems are better executed by different strategies. 7

Strategies A strategy is a mapping from terms to set of steps. When it is applied to a term it computes both a reduct (computed value) and an instantiation (computed answer) of some variables of the term. A useful strategy must have a number of desirable properties that are better explained when the term is an equation with free variables. Soundness: every computed answer is a solution of the equation. Completeness: every solution of the equation is computed. Efficiency: computational resources are not wasted. 8

Classes of Rewrite Systems CB OIS WO IS 9

Inductively Sequential TRSs Constructor-based subclass of the strongly sequential TRSs take 0 - -> [] take (s N) [] -> [] take (s N) [X X s ] -> [X take N X s ] The left-hand sides of the rules of each operation can be organized a in hierarchical structure called a definitional tree take N X take 0 X take (s N 1 ) X take (s N 1 ) [] take (s N 1 ) [X 1 X s ] 10

Needed Narrowing The strategy for the inductively sequential TRSs is needed narrowing. Needed narrowing is easily implemented using a definitional tree as an automaton to compute steps. Needed narrowing satisfies two important optimality criteria. Only needed steps are computed: derivations have minimal length. Only needed derivations are computed: substitutions are disjoint. 11

Compute a Needed Step The task is to evaluate t = take N u, where N is an unbound variable and u is some operation-rooted term. Unify t with a maximal (lowest) element of the definitional tree. There are two such elements: take 0 X (a leaf) and take (s N 1 ) X (a branch). Because the first is a leaf, instantiate t with {N 0} and narrow. The result is [] Because the second is a branch, instantiate t with {N (s N 1 )} and recursively evaluate the match of the inductive variable. An instance of u is evaluated. 12

Weakly Orthogonal TRSs Rewrite rules s lhs may overlap, but only if critical pairs are trivial. Interesting TRSs because they capture parallelism. true - -> true - true -> true false false -> false There is no definitional tree of operation. There are terms that have no needed redex. In practice, to evaluate a term of the form u v both u and v must be evaluated concurrently. 13

Parallel Narrowing The strategy for the weakly orthogonal TRSs is parallel narrowing. The rules of a weakly orthogonal operation can be partioned into subsets so that every subset has a definitional tree. The set of steps obtained by computing a step for each subset of the partition is a necessary set of steps. One step of a necessary set of a term t is executed in any computation of t to a constructor term. Parallel narrowing is a conservative extension of both needed narrowing and weakly needed rewriting. 14

Overlapping Inductively Sequential TRSs Rewrite rules s lhs may overlap, but only if they are equal modulo a renaming of variables. Interesting TRSs because they capture non-determinism. regexp X -> X "(" ++ regexp X ++ ")" regexp X ++ regexp X regexp X ++ "*" regexp X ++ " " ++ regexp X To recognize whether a string s is a well-formed regular expression over {0, 1} one evaluates regex ( 0 1 ) = s. 15

INS The strategy for the overlapping inductively sequential TRSs is INS. Overlapping inductively sequential operations have a definitional tree. INS steps are computed as needed steps, except that several alternative replacements may be available for a narrex. The optimality results of needed narrowing holds for INS, but only modulo non-deterministic choices. Needed and possibly non-needed steps and/or derivations are computed by INS. 16

Constructor-Based TRSs No specific restrictions on the rewrite rules except for the constructor discipline and left linearity. Greatest expressive power: permute [] -> [] permute [X X s ] -> insert X (permute X s ) insert X Y s -> [X Y s ] insert X [Y Y s ] -> [Y insert X Y s ] No accepted notion of need. Known strategies are demand-driven and their properties are poorly understood. There exists a semantics-preserving transformation into the overlapping inductively sequential TRSs. 17

Transforming Conditions The conditional part of a rule is a sequence of equational constraints: l r u 1 = v 1,..., u n = v n expressed by ordinary operations: conjunction, strict equality,... A new conditional operation is added to the signature: if success then X X Rules are deconditionalized by moving the condition to the rhs in the form of a conditional expression: l if u 1 = v 1,..., u n = v n then r 18

Transforming Overlapping The set of rules of an operation f is partitioned into overlapping inductively sequential subsets. Each subset of a partition defines a new overlapping inductively sequential operation, say f 1, f 2,..., f n. Operation f is replaced by f( X) f 1 ( X) f n ( X) The choice to evaluate an argument of an operation is transformed into a choice of one of several rhs s. 19

Transformation Example The following operation: insert X Y s -> [X Y s ] insert X [Y Y s ] -> [Y insert X Y s ] is transformed into: insert X Y s -> insert 1 X Y s insert 2 X Y s insert 1 X Y s -> [X Y s ] insert 2 X [Y Y s ] -> [Y insert X Y s ] Some optimizations are possible, e.g., insert 1 can be eliminated. 20

Transformation Properties Let R be a left-linear constructor-based TRS and S the transformed TRS. S is an overlapping inductively sequential TRS. The constructor terms of R and S are the same. For each computation t v in R there exists a computation t v in S. Conclusion: all the computations of R are simulated by computations of S. 21

Summary Narrowing and rewriting strategies for the constructor-based TRSs have been extensively investigated and are well understood. Four subclasses are emerging for functional logic programming. Each class has at least one effective strategy. Inductively Sequential: functional computations, needed narrowing. Weakly Orthogonal: parallel computations, parallel narrowing. Overlapping Inductively Sequential: INS. non-deterministic computations, Left-Linear: transform into overlapping inductively sequential. 22

Acknowledgments The organization of the International Workshop on Reduction Strategies in Rewriting and Programming (WRS 2001) gave me the opportunity me to work on this survey and invited me to present this talk. The National Science Foundation supported in part this research under grants INT-9981317 and CCR-9406751. 23