A Tabling Implementation Based on Variables with Multiple Bindings an 1 Manuel Carro 1 Pablo Chico de Guzm´ Manuel V. Hermenegildo 1 , 2 1 School of Computer Science, Technical University of Madrid, Spain 2 IMDEA-Software, Spain ICLP 2009 — Pasadena, CA, USA – July 15, 2009 Pablo Chico de Guzm´ an et al (UPM) A Tabling Implementation. . . ICLP 2009, Pasadena, CA 1 / 1
Motivation Motivation Tabling : resolution strategy (alternative to SLD) aimed at improving both efficiency and declarativeness in Prolog. ◮ ≈ memoing: answers for already executed calls retrieved from table. ◮ Also, infinite recursions avoided by delaying the loop-creating call. Many applications: deductive databases, program analysis, reasoning in the semantic Web, model checking, . . . ?− p(A). Example code :- table p/1. q, p(B), A = 2. A = 1. p(A) :- p(B), A = 2. q, p(B), A = 2. Suspension p(1), A = 2. p(A) :- A = 2. A = 1. Pablo Chico de Guzm´ an et al (UPM) A Tabling Implementation. . . ICLP 2009, Pasadena, CA 2 / 1
Motivation Main Implementation Approaches Linear tabling : ◮ Fails on looping branches, performs recomputation instead of resuming. ◮ Only one execution tree ⇒ lower memory stack usage. Suspension-based mechanisms : ◮ Saves status of looping call to be resumed later. ◮ Several execution trees ⇒ higher memory stack usage. ?− p(A). Example code :- table p/1. q, p(B), A = 2. A = 1. p(A) :- p(B), A = 2. q, p(B), A = 2. Suspension p(1), A = 2. p(A) :- A = 2. A = 1. Pablo Chico de Guzm´ an et al (UPM) A Tabling Implementation. . . ICLP 2009, Pasadena, CA 3 / 1
Suspension-Based Implementations Suspension-Based Implementations CHAT Example Really good memory usage: ◮ Sharing local stack and heap. ◮ Copying those bindings which are different for each consumer. CHOICE LOCAL HEAP STACK POINTS G A 1 TRAIL P B 2 C Pablo Chico de Guzm´ an et al (UPM) A Tabling Implementation. . . ICLP 2009, Pasadena, CA 4 / 1
Suspension-Based Implementations Suspension-Based Implementations CHAT Example Really good memory usage: ◮ Sharing local stack and heap. ◮ Copying those bindings which are different for each consumer. CHOICE LOCAL HEAP STACK POINTS G A 1 TRAIL @A P 1 @B C 2 B 2 CHAT AREA C Pablo Chico de Guzm´ an et al (UPM) A Tabling Implementation. . . ICLP 2009, Pasadena, CA 4 / 1
Suspension-Based Implementations Suspension-Based Implementations CHAT Example Really good memory usage: ◮ Sharing local stack and heap. ◮ Copying those bindings which are different for each consumer. CHOICE LOCAL HEAP STACK POINTS G A TRAIL @A 1 @B C 2 B CHAT AREA Pablo Chico de Guzm´ an et al (UPM) A Tabling Implementation. . . ICLP 2009, Pasadena, CA 4 / 1
Suspension-Based Implementations Suspension-Based Implementations CHAT Example Really good memory usage: ◮ Sharing local stack and heap. ◮ Copying those bindings which are different for each consumer. CHOICE LOCAL HEAP STACK POINTS G C A 1 TRAIL @A 1 @B C 2 B 2 CHAT AREA Pablo Chico de Guzm´ an et al (UPM) A Tabling Implementation. . . ICLP 2009, Pasadena, CA 4 / 1
Suspension-Based Implementations Suspension-Based Implementations Speculative Work Bindings are reinstalled for the consumer execution (and later unbound): ◮ They may be not used ⇒ speculative work ! HEAP Code _1 :− table t/1. _2 t(X) :− _3 large execution (with huge trail), t(X), X = 1000. t(1). t(2). _N−1 _N t(1000). Pablo Chico de Guzm´ an et al (UPM) A Tabling Implementation. . . ICLP 2009, Pasadena, CA 5 / 1
Suspension-Based Implementations Suspension-Based Implementations Speculative Work Bindings are reinstalled for the consumer execution (and later unbound): ◮ They may be not used ⇒ speculative work ! HEAP Code _1 1 :− table t/1. _2 2 t(X) :− _3 3 large execution (with huge trail), t(X), X = 1000. t(1). t(2). _N−1 N−1 _N N t(1000). Pablo Chico de Guzm´ an et al (UPM) A Tabling Implementation. . . ICLP 2009, Pasadena, CA 5 / 1
Suspension-Based Implementations Suspension-Based Implementations Speculative Work Bindings are reinstalled for the consumer execution (and later unbound): ◮ They may be not used ⇒ speculative work ! HEAP CHAT AREA Code _1 _1 1 :− table t/1. 1 _2 2 t(X) :− _2 _3 3 large execution 2 (with huge trail), t(X), X = 1000. t(1). t(2). _N−1 N−1 _N N _N N t(1000). Pablo Chico de Guzm´ an et al (UPM) A Tabling Implementation. . . ICLP 2009, Pasadena, CA 5 / 1
Suspension-Based Implementations Suspension-Based Implementations Speculative Work Bindings are reinstalled for the consumer execution (and later unbound): ◮ They may be not used ⇒ speculative work ! HEAP CHAT AREA Code _1 _1 :− table t/1. 1 _2 t(X) :− _2 _3 large execution 2 (with huge trail), t(X), X = 1000. t(1). t(2). _N−1 _N N _N t(1000). Pablo Chico de Guzm´ an et al (UPM) A Tabling Implementation. . . ICLP 2009, Pasadena, CA 5 / 1
Suspension-Based Implementations Suspension-Based Implementations Speculative Work Bindings are reinstalled for the consumer execution (and later unbound): ◮ They may be not used ⇒ speculative work ! HEAP CHAT AREA Code _1 _1 1 :− table t/1. 1 _2 2 t(X) :− _2 _3 3 large execution 2 (with huge trail), t(X), X = 1000. t(1). t(2). _N−1 N−1 _N N _N N t(1000). Pablo Chico de Guzm´ an et al (UPM) A Tabling Implementation. . . ICLP 2009, Pasadena, CA 5 / 1
Suspension-Based Implementations Suspension-Based Implementations Speculative Work Bindings are reinstalled for the consumer execution (and later unbound): ◮ They may be not used ⇒ speculative work ! HEAP CHAT AREA Code _1 _1 :− table t/1. 1 _2 t(X) :− _2 _3 large execution 2 (with huge trail), t(X), X = 1000. t(1). t(2). _N−1 _N N _N t(1000). Pablo Chico de Guzm´ an et al (UPM) A Tabling Implementation. . . ICLP 2009, Pasadena, CA 5 / 1
MVB Implementation Basic Idea of MVB Local stack and Heap frozen as CHAT does. MVB variables keep track of several bindings. The proper binding is accessed using a consumer id . Adv.: bindings are not reinstalled/unbound when a consumer resumes/fails (there is no speculative work). Disadv.: variable access more complex, and may be non-constant time. HEAP { Consumer A 1 Consumer B 2 MVB Consumer C 3 4 Consumer D Pablo Chico de Guzm´ an et al (UPM) A Tabling Implementation. . . ICLP 2009, Pasadena, CA 6 / 1
MVB Implementation MVB as List of Values Structure: CACHE MVB Last Id Last Value C1 1 C2 2 C3 3 Behavior: Measure sgm atr2 pg kalah gabriel disj cs o peep Avg. Accesses 150412 36247 886 562 1720 1084 508 5876 — Misses 1504 2545 147 100 155 103 33 335 — % Misses 1 7 16.6 17.8 9 9.5 6.5 5.7 8.7 Avg.Length 30.6 1 3.5 1.4 1.8 1.3 1.2 2.1 4.9 Avg.Trav. 15.8 1 11.5 1.3 3.8 2.5 1.8 5.4 5 Seems efficient enough —at least in our benchmarks- ◮ Balanced trees, arrays, hash tables. . . also possible. Pablo Chico de Guzm´ an et al (UPM) A Tabling Implementation. . . ICLP 2009, Pasadena, CA 7 / 1
MVB Implementation ID = 0 MVB Execution Example ?− t(X). A 1.− p(A), t(X), A = 1. Code :− table t/1. 2.− t(X), 2 = 1. t(X) :− p(A), suspension ID = 1 t(X), A = 1000. t(1). p(2). p(3). Pablo Chico de Guzm´ an et al (UPM) A Tabling Implementation. . . ICLP 2009, Pasadena, CA 8 / 1
MVB Implementation ID = 0 MVB Execution Example ?− t(X). A 2 1.− p(A), t(X), A = 1. Code :− table t/1. 2.− t(X), 2 = 1. t(X) :− p(A), suspension ID = 1 t(X), A = 1000. t(1). p(2). p(3). Pablo Chico de Guzm´ an et al (UPM) A Tabling Implementation. . . ICLP 2009, Pasadena, CA 8 / 1
MVB Implementation ID = 0 MVB Execution Example ?− t(X). CACHE MVB A 1 2 0 1.− p(A), t(X), A = 1. Code :− table t/1. 2.− t(X), 2 = 1. t(X) :− p(A), suspension ID = 1 t(X), A = 1000. t(1). p(2). p(3). Pablo Chico de Guzm´ an et al (UPM) A Tabling Implementation. . . ICLP 2009, Pasadena, CA 8 / 1
MVB Implementation ID = 0 MVB Execution Example ?− t(X). CACHE MVB A 1 2 0 3 1.− p(A), t(X), A = 1. Code :− table t/1. 2.− t(X), 2 = 1. 3.− t(X), 3 = 1. t(X) :− p(A), suspension suspension ID = 2 ID = 1 t(X), A = 1000. t(1). p(2). p(3). Pablo Chico de Guzm´ an et al (UPM) A Tabling Implementation. . . ICLP 2009, Pasadena, CA 8 / 1
MVB Implementation ID = 0 MVB Execution Example ?− t(X). CACHE MVB A 2 3 1 2 0 1.− p(A), t(X), A = 1. Code :− table t/1. 2.− t(X), 2 = 1. 3.− t(X), 3 = 1. t(X) :− p(A), suspension suspension ID = 2 ID = 1 t(X), A = 1000. t(1). p(2). p(3). Pablo Chico de Guzm´ an et al (UPM) A Tabling Implementation. . . ICLP 2009, Pasadena, CA 8 / 1
MVB Implementation ID = 0 MVB Execution Example ?− t(X). CACHE MVB A 2 3 1 2 0 1.− p(A), t(X), A = 1. 4.− t(1). Code :− table t/1. 2.− t(X), 2 = 1. 3.− t(X), 3 = 1. t(X) :− p(A), suspension suspension ID = 2 ID = 1 t(X), A = 1000. t(1). p(2). p(3). Pablo Chico de Guzm´ an et al (UPM) A Tabling Implementation. . . ICLP 2009, Pasadena, CA 8 / 1
Recommend
More recommend