a syntactic approach to combining functional notation
play

A Syntactic Approach to Combining Functional Notation, Lazy - PowerPoint PPT Presentation

Slide 0 A Syntactic Approach to Combining Functional Notation, Lazy Evaluation, and Higher-Order in LP Systems Amadeo Casas 1 Daniel Cabeza 2 Manuel Hermenegildo 1 , 2 { amadeo, herme } @cs.unm.edu { dcabeza, herme } @fi.upm.es 1 Depts. of Comp.


  1. Slide 0 A Syntactic Approach to Combining Functional Notation, Lazy Evaluation, and Higher-Order in LP Systems Amadeo Casas 1 Daniel Cabeza 2 Manuel Hermenegildo 1 , 2 { amadeo, herme } @cs.unm.edu { dcabeza, herme } @fi.upm.es 1 Depts. of Comp. Science and Electr. and Comp. Eng. Univ. of New Mexico, Albuquerque, NM, USA. 2 School of Computer Science T. U. Madrid (UPM), Madrid, Spain CLIP Group A Syntactic Approach to Combining Functions, Lazy Evaluation, and HO in LP FLOPS’06, Fuji Susono, April 24–26, 2006

  2. Slide 1 Introduction LP offers features, such as nondeterminism, partially instantiated data structures, and constraints providing high expressive power. FP provides syntactic convenience (because of designated output argument). FP also provides lazy evaluation: ability to deal with infinite data structures and save execution steps. LP provides more powerful (lazy) evaluation mechanism (delay declarations) but, again, FP brings syntactic convenience. Discuss the combination with higher order. We present a syntactic functional layer (combining functions, laziness, and HO) as implemented in the Ciao language (but useful in general for LP-based systems). A Syntactic Approach to Combining Functions, Lazy Evaluation, and HO in LP FLOPS’06, Fuji Susono, April 24–26, 2006

  3. Slide 2 Is any of this new? Adding functional features to LP systems clearly not new: A good number of systems integrate functions into some form of LP: NU-Prolog, Lambda-Prolog, HiLog/XSB, Oz, Mercury, HAL, . . . Or perform a “native” integration of FP and LP (e.g., Babel, Curry, ...). Ciao design is contemporary to these ( ~ 97). Its peculiarities make it interesting: Functions can retain the power of predicates (it is just syntax!). Functions inherit all other Ciao features (assertions, properties, records, constraints, ...) + (analysis, optimization, verification, ACC, ...) . The system can support ISO-Prolog, and functions, laziness, (and hiord) can be used as a extension of it (or not). Also the implementation is different (library-based): Exploits the Ciao extension/restriction facilities: Ciao packages concept. Makes it independent from, and (partially) compositional with other extensions. No compiler or abstract machine modification (all done at source level). A Syntactic Approach to Combining Functions, Lazy Evaluation, and HO in LP FLOPS’06, Fuji Susono, April 24–26, 2006

  4. Slide 3 Functional Notation in Ciao (I) Function applications: Any term preceded by the ~/1 operator is a function application: write(~arg(1, T)). arg(1, T, A), write(A). Declarations can be used to avoid the need to use the ~/1 operator: :- fun eval arg/2. write(arg(1, T)). Also possible to use arguments other than last for “return”: :- fun return functor(~, , ). ~functor(~, f, 2). The following declaration combines the previous two: :- fun return functor(~, , ). :- fun eval functor(~, , ). :- fun eval functor/2. A Syntactic Approach to Combining Functions, Lazy Evaluation, and HO in LP FLOPS’06, Fuji Susono, April 24–26, 2006

  5. Slide 4 Functional Notation in Ciao (II) Several functors are evaluable by default: Special forms for disjunctive and conditional expressions: |/2 and ?/2 . A | B | C Cond1 ? V1 Cond1 ? V1 | V2 Precedence implies that: Cond1 ? V1 | Cond2 ? V2 | V3 is parsed as: Cond1 ? V1 | (Cond2 ? V2 | V3) All the functors understood by is/2 , if the following declaration is used: :- fun_eval arith(true). Using false it can be (selectively) disabled. Functional definitions: opposite(red) := green . ≡ opposite(red,green) . addlast(X,L) := ~append(L,[X]) . ≡ addlast(X,L,R) :- append(L,[X],R) . A Syntactic Approach to Combining Functions, Lazy Evaluation, and HO in LP FLOPS’06, Fuji Susono, April 24–26, 2006

  6. Slide 5 Functional Notation in Ciao (III) Can also have a body (serves as a guard or as where ): fact(0) := 1. fact(N) := N * ~fact(--N) :- N > 0. The declaration :- fun_eval defined(true) . allows dropping the ~ within a function’s definition: fact(0) := 1. fact(N) := N * fact(--N) :- N > 0. And, using conditional expressions: fac(N) := N = 0 ? 1 | N > 0 ? N * fac(--N). The translation: Produces steadfast predicates (bindings after cuts). Maintains tail recursion. A Syntactic Approach to Combining Functions, Lazy Evaluation, and HO in LP FLOPS’06, Fuji Susono, April 24–26, 2006

  7. Slide 6 Deriv and its Translation der(x) := 1. der(C) := 0 :- number(C). der(A + B) := der(A) + der(B). der(C * A) := C * der(A) :- number(C). der(x ** N) := N * x ** ~(N - 1) :- integer(N), N > 0. der(x, 1). der(C, 0) :- number(C). der(A + B, X + Y) :- der(A, X), der(B, Y). der(C * A, C * X) :- number(C), der(A, X). der(x ** N, N * x ** N1) :- integer(N), N > 0, N1 is N - 1. A Syntactic Approach to Combining Functions, Lazy Evaluation, and HO in LP FLOPS’06, Fuji Susono, April 24–26, 2006

  8. Slide 7 Examples – Sugar for Append Some syntactic sugar for append: :- fun_eval append/2. mystring(X) := append("Hello",append(X,"world!")). Some more: :- op(200,xfy,[++]). :- fun_eval ++ /2. A ++ B := ~append(A,B). mystring(X) := "Hello" ++ X ++ "world!". A Syntactic Approach to Combining Functions, Lazy Evaluation, and HO in LP FLOPS’06, Fuji Susono, April 24–26, 2006

  9. Slide 8 Examples – Array Access Syntax Assume multi-dimensional arrays such as (for 2x2): A = a(a(_,_),a(_,_)). We can now define the array access function with some syntactic sugar: :- pred @(Array,Index,Elem) :: array * list(int) * int # "@var{Elem} is the @var{Index}-th element of @var{Array}.". :- op(45, xfx, [@]). :- fun_eval ’@’/2 . @(V,[I]) := ~arg(I,V). @(V,[I|Js]) := @(~arg(I,V),Js). And use it: ?- M = ~array([2,2]), M@[2,1] = 3, display(M). (for this the op and function declarations must be loaded in the top level also!) E.g., in a vector addition: for(I,1,N) do V3@[I] = V1@[I] + V2@[I] A Syntactic Approach to Combining Functions, Lazy Evaluation, and HO in LP FLOPS’06, Fuji Susono, April 24–26, 2006

  10. Slide 9 Functional Notation in Ciao (IV) Quoting. Evaluable functors can be prevented from being evaluated: pair(A,B) := ^(A-B). Scoping. When innermost function application is not desired (e.g., for certain meta-predicates) a different scope can be determined with the (^^)/1 operator: findall(X, (d(Y), ^^(X = ~f(Y)+1)), L) . translates to: findall(X, (d(Y),f(Y,Z),T is Z+1,X=T), L) . as opposed to: f(Y,Z), T is Z+1, findall(X, (d(Y),X=T), L) . Laziness. Execution is suspended until the return value is needed: :- lazy fun_eval nums_from/1. nums_from(X) := [X | nums_from(X+1)]. (Can be done easily with when , block , freeze , etc. but proposed notation more compact for this special case. Also, :- lazy pred_name/M .) A Syntactic Approach to Combining Functions, Lazy Evaluation, and HO in LP FLOPS’06, Fuji Susono, April 24–26, 2006

  11. Slide 10 Functional Notation in Ciao (V) Functional notation really useful, e.g., to write regular types in a compact way: color := red | blue | green. list := [] | [ _ | list]. list_of(T) := [] | [~T | list_of(T)]. Which translate to: color(red). color(blue). color(green). list([]). list([_|T]) :- list(T). list_of(_, []). list_of(T, [X|Xs]) :- T(X), list_of(T, Xs). And can then of course be used in Ciao assertions: :- pred append/3 :: list * list * list. :- pred color_value/2 :: list(color) * int. A Syntactic Approach to Combining Functions, Lazy Evaluation, and HO in LP FLOPS’06, Fuji Susono, April 24–26, 2006

  12. Slide 11 Functional Notation in Ciao (VI) Definition of “real” functions: :- funct name/N. adds pruning operators and Ciao assertions to add functional restrictions: determinacy, modedness, etc. E.g.: :- funct nrev/1. nrev( [] ) := []. nrev( [H|T] ) := ~conc( nrev(T),[H] ). Is translated to (simplified): :- pred nrev(A,B,C) : (ground(A), ground(B), var(C)) => (ground(A), ground(B), ground(C)) + is_det,mut_exclusive,covered,no_fail. nrev( [], Y ) :- !, Y = []. nrev( [H|L],R ) :- !, nrev(L,RL), conc(RL,[H],R). A Syntactic Approach to Combining Functions, Lazy Evaluation, and HO in LP FLOPS’06, Fuji Susono, April 24–26, 2006

  13. Slide 12 Combining with Constraints, etc. Combining with constraints, some syntactic sugar, assertions: :- module(_,_,[assertions,fsyntax,clpq]). :- fun_eval .=. /1. :- op(700,fx,[.=.]). :- fun_eval fact/1. :- pred fact(+int,-int) + is_det. :- pred fact(-int,-int) + non_det. fact( .=. 0) := 1. fact(N) := .=. N*fact( .=. N-1 ) :- N .>. 0. Sample query: ?- 24 = ~fact(X). X = 4 A Syntactic Approach to Combining Functions, Lazy Evaluation, and HO in LP FLOPS’06, Fuji Susono, April 24–26, 2006

  14. Slide 13 Combining Higher-Order with Functional Notation HO not topic of the paper, but combines well with these syntactic extensions. Combining function application ( ~ ) and HO: Predicate application ⇒ Function application ⇒ ..., Y = ~P(X), ... ..., P(X,Y), ... Function abstraction: Predicate abstraction ⇒ Function abstraction {’’(X,Y) :- p(X,Z), q(Z,Y)} ⇒ {’’(X) := ~q(~p(X))} The integration is at the predicate level. A Syntactic Approach to Combining Functions, Lazy Evaluation, and HO in LP FLOPS’06, Fuji Susono, April 24–26, 2006

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend