2P-Kt: logic programming with objects & functions
in Kotlin
Giovanni Ciatto∗ Roberta Calegari◦ Enrico Siboni†
Enrico Denti‡ Andrea Omicini
∗‡ Dipartimento di Informatica – Scienza e Ingegneria (DISI)
◦Alma Mater Research Institute for Human-Centered Artificial Intelligence
Alma Mater Studiorum—Universit`a di Bologna, Italy
{giovanni.ciatto, roberta.calegari, enrico.denti, andrea.omicini}@unibo.it
†University of Applied Sciences and Arts of Western Switzerland (HES-SO)
enrico.siboni@hevs.ch
21st Workshop “From Objects to Agents” (WOA)
Sept. 16, 2020, Bologna, Italy
Ciatto et al. (UniBo, HES-SO) 2P-Kt: LP in Kotlin WOA, 2020 1 / 22
Motivation & Context
Next in Line. . .
1 Motivation & Context
2 Kotlin DSL for Prolog
3 Behind the scenes
4 Conclusions & future works
Ciatto et al. (UniBo, HES-SO) 2P-Kt: LP in Kotlin WOA, 2020 1 / 22
Motivation & Context
Context
New opportunities for logic-based technologies: (X)AI and MAS
AI side
AI is shining, brighter than ever
mostly thanks to the advances in ML and sub-symbolic AI
⇒ symbolic AI is gaining momentum because of eXplanable AI (XAI)
! hybrid solution mixing logic & data-driven AI are flourishing [3]
MAS side
The MAS community is eager for logic-based technologies [2]
to support agents’ knowledge representation, reasoning, or execution
or to prove MAS properties
! despite few mature tech. exist, and even fewer are actively maintained
Ciatto et al. (UniBo, HES-SO) 2P-Kt: LP in Kotlin WOA, 2020 2 / 22
Motivation & Context
Motivation
The problem with logic-based technologies
There is technological barrier slowing
the adoption of logic programming (LP) as paradigm
the exploitation of logic-based technologies
while programming in the large
e.g. Scala, Kotlin, Python, C#
mainstream programming languages are blending several paradigms
e.g. imperative, object-oriented (OOP), and functional programming (FP)
except LP!
mainstream platforms
e.g. JVM, .NET, JS, Python
are poorly interoperable with logic-based tech.
Ciatto et al. (UniBo, HES-SO) 2P-Kt: LP in Kotlin WOA, 2020 3 / 22
Motivation & Context
Motivating example – SWI-Prolog’s FLI for Java
Prolog [4] implementors rely on Foreign Language Interfaces (FLI) [1]
(mostly targetting Java, or C)
For instance, SWI-Prolog comes with a FLI for Java1:
Query query = new Query("parent", new Term[] {
new Atom("adam"),
new Variable("X")
}
); // ?- parent(adam, X).
Map<String,Term> solution = query.oneSolution();
System.out.println("The child of Adam is " + solution.get("X"));
→ No paradigm harmonization between Prolog and the hosting language
i.e. Java
1
https://jpl7.org
Ciatto et al. (UniBo, HES-SO) 2P-Kt: LP in Kotlin WOA, 2020 4 / 22
Motivation & Context
Contribution of the paper
Show that OOP, FP, and LP can be blended into a single language
Propose a DSL blending Kotlin (OOP + FP) and Prolog (LP)
! DSL = domain specific language
Pave the way to the creation of similar DSL in mainstream languages
eg Scala
Ciatto et al. (UniBo, HES-SO) 2P-Kt: LP in Kotlin WOA, 2020 5 / 22
Kotlin DSL for Prolog
Next in Line. . .
1 Motivation & Context
2 Kotlin DSL for Prolog
3 Behind the scenes
4 Conclusions & future works
Ciatto et al. (UniBo, HES-SO) 2P-Kt: LP in Kotlin WOA, 2020 5 / 22
Kotlin DSL for Prolog Overview
Focus on. . .
1 Motivation & Context
2 Kotlin DSL for Prolog
Overview
Principles
Functioning
Advantages
3 Behind the scenes
Overview
2P-Kt
Kotlin
2P-Kt + Kotlin
4 Conclusions & future works
Ciatto et al. (UniBo, HES-SO) 2P-Kt: LP in Kotlin WOA, 2020 5 / 22
Kotlin DSL for Prolog Overview
Whet your appetite
Our Kotlin DSL for Prolog vs. actual Prolog
prolog {
staticKb(
rule {
"ancestor"("X", "Y") `if` "parent"("X", "Y") // ancestor(X, Y) :- parent(X, Y).
}, //
rule { //
"ancestor"("X", "Y") `if` ( // ancestor(X, Y) :-
"parent"("X", "Z") and "ancestor"("Z", "Y") // parent(X, Z), ancestor(Z, Y).
) //
}, //
fact { "parent"("abraham", "isaac") }, // parent(abraham, isaac).
fact { "parent"("isaac", "jacob") }, // parent(isaac, jacob).
fact { "parent"("jacob", "joseph") } // parent(jacob, joseph).
) //
//
for (sol in solve("ancestor"("abraham", "D"))) // ?- ancestor(abraham, D),
if (sol is Solution.Yes) // write(D), nl.
println(sol.substitution["D"])
}
! try it here: https://github.com/tuProlog/prolog-dsl-example
Ciatto et al. (UniBo, HES-SO) 2P-Kt: LP in Kotlin WOA, 2020 6 / 22
Kotlin DSL for Prolog Principles
Focus on. . .
1 Motivation & Context
2 Kotlin DSL for Prolog
Overview
Principles
Functioning
Advantages
3 Behind the scenes
Overview
2P-Kt
Kotlin
2P-Kt + Kotlin
4 Conclusions & future works
Ciatto et al. (UniBo, HES-SO) 2P-Kt: LP in Kotlin WOA, 2020 6 / 22
Kotlin DSL for Prolog Principles
Design Principles
P1 – The DSL strictly extends the hosting language
→ no feature of the hosting language is forbidden within the DSL
P2 – The DSL is interoperable with hosting language
→ all features of the hosting language are allowed within the DSL
→ LP is harmonised with the hosting language paradigm(s)
P3 – The DSL is well encapsulated within the hosting language
→ i.e. only usable within well-identifiable sections
P4 – The DSL is as close as possible to Prolog
→ both syntactically & semantically
Ciatto et al. (UniBo, HES-SO) 2P-Kt: LP in Kotlin WOA, 2020 7 / 22
Kotlin DSL for Prolog Principles
Design Principles
P1 – The DSL strictly extends the hosting language
→ no feature of the hosting language is forbidden within the DSL
P2 – The DSL is interoperable with hosting language
→ all features of the hosting language are allowed within the DSL
→ LP is harmonised with the hosting language paradigm(s)
P3 – The DSL is well encapsulated within the hosting language
→ i.e. only usable within well-identifiable sections
P4 – The DSL is as close as possible to Prolog
→ both syntactically & semantically
Ciatto et al. (UniBo, HES-SO) 2P-Kt: LP in Kotlin WOA, 2020 7 / 22
Kotlin DSL for Prolog Principles
Design Principles
P1 – The DSL strictly extends the hosting language
→ no feature of the hosting language is forbidden within the DSL
P2 – The DSL is interoperable with hosting language
→ all features of the hosting language are allowed within the DSL
→ LP is harmonised with the hosting language paradigm(s)
P3 – The DSL is well encapsulated within the hosting language
→ i.e. only usable within well-identifiable sections
P4 – The DSL is as close as possible to Prolog
→ both syntactically & semantically
Ciatto et al. (UniBo, HES-SO) 2P-Kt: LP in Kotlin WOA, 2020 7 / 22
Kotlin DSL for Prolog Principles
Design Principles
P1 – The DSL strictly extends the hosting language
→ no feature of the hosting language is forbidden within the DSL
P2 – The DSL is interoperable with hosting language
→ all features of the hosting language are allowed within the DSL
→ LP is harmonised with the hosting language paradigm(s)
P3 – The DSL is well encapsulated within the hosting language
→ i.e. only usable within well-identifiable sections
P4 – The DSL is as close as possible to Prolog
→ both syntactically & semantically
Ciatto et al. (UniBo, HES-SO) 2P-Kt: LP in Kotlin WOA, 2020 7 / 22
Kotlin DSL for Prolog Functioning
Focus on. . .
1 Motivation & Context
2 Kotlin DSL for Prolog
Overview
Principles
Functioning
Advantages
3 Behind the scenes
Overview
2P-Kt
Kotlin
2P-Kt + Kotlin
4 Conclusions & future works
Ciatto et al. (UniBo, HES-SO) 2P-Kt: LP in Kotlin WOA, 2020 7 / 22
Kotlin DSL for Prolog Functioning
Functioning in the Kotlin case I
The DSL is only enabled within prolog { DSL block } expressions
there, Kotlin objects are automatically converted into Prolog terms
Expressions of the form: "functor"( e1 , e2 , . . .)
are interpreted as terms: functor(t1, t2, . . .)
provided that ∀i : ei can be converted into ti
Ciatto et al. (UniBo, HES-SO) 2P-Kt: LP in Kotlin WOA, 2020 8 / 22
Kotlin DSL for Prolog Functioning
Functioning in the Kotlin case I
The DSL is only enabled within prolog { DSL block } expressions
there, Kotlin objects are automatically converted into Prolog terms
Expressions of the form: "functor"( e1 , e2 , . . .)
are interpreted as terms: functor(t1, t2, . . .)
provided that ∀i : ei can be converted into ti
Ciatto et al. (UniBo, HES-SO) 2P-Kt: LP in Kotlin WOA, 2020 8 / 22
Kotlin DSL for Prolog Functioning
Functioning in the Kotlin case II
Expressions of the form:
rule {"head"( e1 , . . ., eN ) `if` ( eN+1 and . . . and eM ) }
are interpreted as rules: head(t1, . . . , tN) :- tN+1, . . . , tM
provided that ∀i : ei can be converted into ti
similar syntax for facts
Ciatto et al. (UniBo, HES-SO) 2P-Kt: LP in Kotlin WOA, 2020 9 / 22
Kotlin DSL for Prolog Functioning
Functioning in the Kotlin case III
Within prolog { . . . } blocks
staticKb(Clause1, Clause2, ...) sets up the local static KB
dynamicKb(Clause1, Clause2, ...) sets up the local dynamic KB
solve(Query, Timeout) returns a lazy stream of solutions
assert(Clause) appends a new clause to the local dynamic KB
. . .
Ciatto et al. (UniBo, HES-SO) 2P-Kt: LP in Kotlin WOA, 2020 10 / 22
Kotlin DSL for Prolog Functioning
Kotlin to Prolog conversions
Kotlin Object Type Prolog Term Type
lowercase string atom
uppercase string variable
int, long, short, byte integer
double, float real
boolean atom
list, array, iterable list
Ciatto et al. (UniBo, HES-SO) 2P-Kt: LP in Kotlin WOA, 2020 11 / 22
Kotlin DSL for Prolog Advantages
Focus on. . .
1 Motivation & Context
2 Kotlin DSL for Prolog
Overview
Principles
Functioning
Advantages
3 Behind the scenes
Overview
2P-Kt
Kotlin
2P-Kt + Kotlin
4 Conclusions & future works
Ciatto et al. (UniBo, HES-SO) 2P-Kt: LP in Kotlin WOA, 2020 11 / 22
Kotlin DSL for Prolog Advantages
Advantages of a Prolog-like DSL
No further parser / code generator / compiler is needed
Features of the hosting language can be exploited if Prolog falls short
Support tools of the hosting language can be exploited for Prolog
eg debugger, type checker, IDE, etc
Bring the power of LP to mainstream programming
Ciatto et al. (UniBo, HES-SO) 2P-Kt: LP in Kotlin WOA, 2020 12 / 22
Behind the scenes
Next in Line. . .
1 Motivation & Context
2 Kotlin DSL for Prolog
3 Behind the scenes
4 Conclusions & future works
Ciatto et al. (UniBo, HES-SO) 2P-Kt: LP in Kotlin WOA, 2020 12 / 22
Behind the scenes Overview
Focus on. . .
1 Motivation & Context
2 Kotlin DSL for Prolog
Overview
Principles
Functioning
Advantages
3 Behind the scenes
Overview
2P-Kt
Kotlin
2P-Kt + Kotlin
4 Conclusions & future works
Ciatto et al. (UniBo, HES-SO) 2P-Kt: LP in Kotlin WOA, 2020 12 / 22
Behind the scenes Overview
Recipe for a Prolog-like DSL
1 A language with a flexible API
e.g. Kotlin, Scala, Python, Groovy, etc.
2 Full fledged API for Prolog, supporting that language
e.g. 2P-Kt ! see next slide
3 Leverage flexibility to hide the exploitation of the API
Ciatto et al. (UniBo, HES-SO) 2P-Kt: LP in Kotlin WOA, 2020 13 / 22
Behind the scenes 2P-Kt
Focus on. . .
1 Motivation & Context
2 Kotlin DSL for Prolog
Overview
Principles
Functioning
Advantages
3 Behind the scenes
Overview
2P-Kt
Kotlin
2P-Kt + Kotlin
4 Conclusions & future works
Ciatto et al. (UniBo, HES-SO) 2P-Kt: LP in Kotlin WOA, 2020 13 / 22
Behind the scenes 2P-Kt
2P-Kt – Overview
Comprehensive, modular, re-usable API covering most aspects of LP:
unification
Unificator
mgu(Term, Term): Substitution
unify(Term, Term): Term?
match(Term, Term): Boolean
terms
Term
apply(Substitution): Term
freshCopy(): Term
Constant
value: Any
Var
name: String
Struct
functor: String
args: Array<Term>
Numeric
Integer
value: Int
Real
value: Double
Atom
value: String
Truth
isTrue: Boolean
EmptyList
List
Cons
head: Term
tail: Term
clauses & theories
Clause
head: Struct?
body: Term
RuleDirective Fact
Theory
assertA(Clause)
assertZ(Clause)
retract(Clause)
retractAll(Clause)
get(Clause): Sequence<Clause>
1
*
resolution
Solver
solve(Struct,Long): Sequence<Solution>
MutableSolver
loadStaticKb(Theory)
loadDynamicKb(Theory)
Solution
query: Struct
Yes
substitution: Unifier
No
Halt
exception: PrologError
substitutions
Substitution
get(Var): Term
contains(Var): Boolean
Unifier Fail
runs on several platforms (e.g., JVM, NodeJS, Browsers, Android)
more info here: https://github.com/tuProlog/2p-kt
Ciatto et al. (UniBo, HES-SO) 2P-Kt: LP in Kotlin WOA, 2020 14 / 22
Behind the scenes Kotlin
Focus on. . .
1 Motivation & Context
2 Kotlin DSL for Prolog
Overview
Principles
Functioning
Advantages
3 Behind the scenes
Overview
2P-Kt
Kotlin
2P-Kt + Kotlin
4 Conclusions & future works
Ciatto et al. (UniBo, HES-SO) 2P-Kt: LP in Kotlin WOA, 2020 14 / 22
Behind the scenes Kotlin
Kotlin mechanisms for DSL I
We exploited 4 basic mechanisms:
1 Operator overloading
interface Term {
operator fun plus(other: Term): Struct =
Struct.of("+", this, other)
}
// now one can write:
val term3: Term = term1 + term2
Ciatto et al. (UniBo, HES-SO) 2P-Kt: LP in Kotlin WOA, 2020 15 / 22
Behind the scenes Kotlin
Kotlin mechanisms for DSL II
2 Block-like lambda expressions
solutions.filter({ it -> it is Solution.Yes })
.map({ it -> it.substitution["X"] })
.joinToString(" ", { it.toString() })
// ↑ can be rewritten as ↓
solutions.filter { it is Solution.Yes }
.map { it.substitution["X"] }
.joinToString(" ") { it.toString() }
Ciatto et al. (UniBo, HES-SO) 2P-Kt: LP in Kotlin WOA, 2020 16 / 22
Behind the scenes Kotlin
Kotlin mechanisms for DSL III
3 Extension methods
fun Any.toTerm(): Term = // converts Kotlin objects into terms
operator fun String.invoke(vararg args: Term): Struct =
Struct.of(this, args.map { it.toTerm() })
// now one can write
"member"("X", arrayOf(1 .. 3)) // member(X, [1, 2, 3])
Ciatto et al. (UniBo, HES-SO) 2P-Kt: LP in Kotlin WOA, 2020 17 / 22
Behind the scenes Kotlin
Kotlin mechanisms for DSL IV
4 Function types/lambdas with receivers
class PrologDsl {
fun Any.toTerm(): Term = // ...
operator fun String.invoke(vararg args: Term): Struct = // ...
operator fun Any.plus(other: Any): Struct =
this.toTerm() + other.toTerm()
}
// ↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓ type with receiver
fun prolog(action: PrologDsl.() -> Any): Any =
PrologDsl().action()
// ↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓ lambda with receiver
prolog { "f"("1") + "f"("2") } // in braces this is PrologDsl
Ciatto et al. (UniBo, HES-SO) 2P-Kt: LP in Kotlin WOA, 2020 18 / 22
Behind the scenes Kotlin
Kotlin mechanisms for DSL V
This is a Kotlin-specific solution!
Other languages may support DSL through different mechanisms
e.g. implicits in Scala
Ciatto et al. (UniBo, HES-SO) 2P-Kt: LP in Kotlin WOA, 2020 19 / 22
Behind the scenes 2P-Kt + Kotlin
Focus on. . .
1 Motivation & Context
2 Kotlin DSL for Prolog
Overview
Principles
Functioning
Advantages
3 Behind the scenes
Overview
2P-Kt
Kotlin
2P-Kt + Kotlin
4 Conclusions & future works
Ciatto et al. (UniBo, HES-SO) 2P-Kt: LP in Kotlin WOA, 2020 19 / 22
Behind the scenes 2P-Kt + Kotlin
DSL design on top of 2P-Kt
JVM JS
Kotlin
2P-Kt
Prolog DSL
Scope
_: Var
varOf(String): Var
atomOf(String): Atom
structOf(String, Iterable<Term>): Struct
emptyList(): EmptyList
listOf(Iterable<Term>): List
numOf(Double): Real
numOf(Int): Integer
Prolog
Any.toTerm(): Term
String.invoke(Any, vararg Any): Struct
Substitution.get(String): Term?
PrologWithUnification
Any.mguWith(Any): Substitution
Any.matches(Any): Boolean
Any.unifyWith(Any): Term?
PrologWithTheories
theoryOf(vararg Clause): Theory
rule(PrologWithTheories.() -> Any): Rule
fact(PrologWithTheories.() -> Any): Fact
and(Any, Any): Term
or(Any, Any): Term
PrologWithResolution
solve(Any): Sequence<Solution>
solverOf(...)
member(Any, Any): Term
is(Any, Any): Term
!: Atom
fail: Atom
extends
Onion design with incremental features
Built on top of 2P-Kt and Kotlin
Ciatto et al. (UniBo, HES-SO) 2P-Kt: LP in Kotlin WOA, 2020 20 / 22
Conclusions & future works
Next in Line. . .
1 Motivation & Context
2 Kotlin DSL for Prolog
3 Behind the scenes
4 Conclusions & future works
Ciatto et al. (UniBo, HES-SO) 2P-Kt: LP in Kotlin WOA, 2020 20 / 22
Conclusions & future works
Conclusions & future works
Summing up, in this study we. . .
argued LP should be integrated in modern languages/paradigms
designed an in-language, DSL-based solution
prototyped an actual Kotlin-based DSL for Prolog
In the future, we will try to. . .
design Prolog-like DSL for other languages (e.g. Scala)
design an agent-oriented (possibly BDI?) DSL
extend 2P-Kt to support other sorts of inference mechanisms
design a logic-based API for sub-symbolic AI
Ciatto et al. (UniBo, HES-SO) 2P-Kt: LP in Kotlin WOA, 2020 21 / 22
2P-Kt: logic programming with objects & functions
in Kotlin
Giovanni Ciatto∗ Roberta Calegari◦ Enrico Siboni†
Enrico Denti‡ Andrea Omicini
∗‡ Dipartimento di Informatica – Scienza e Ingegneria (DISI)
◦Alma Mater Research Institute for Human-Centered Artificial Intelligence
Alma Mater Studiorum—Universit`a di Bologna, Italy
{giovanni.ciatto, roberta.calegari, enrico.denti, andrea.omicini}@unibo.it
†University of Applied Sciences and Arts of Western Switzerland (HES-SO)
enrico.siboni@hevs.ch
21st Workshop “From Objects to Agents” (WOA)
Sept. 16, 2020, Bologna, Italy
Ciatto et al. (UniBo, HES-SO) 2P-Kt: LP in Kotlin WOA, 2020 22 / 22
Bibliography
References I
[1] Roberto Bagnara and Manuel Carro.
Foreign language interfaces for Prolog: A terse survey.
ALP Newsletter, 15(2), May 2002.
[2] Roberta Calegari, Giovanni Ciatto, Viviana Mascardi, and Andrea Omicini.
Logic-based technologies for multi-agent systems: A systematic literature review.
Autonomous Agents and Multi-Agent Systems, In press.
Special Issue “Current Trends in Research on Software Agents and Agent-Based
Software Development”.
[3] Roberta Calegari, Giovanni Ciatto, and Andrea Omicini.
On the integration of symbolic and sub-symbolic techniques for XAI: A survey.
Intelligenza Artificiale, In press.
[4] Alain Colmerauer and Philippe Roussel.
The birth of prolog.
In John A. N. Lee and Jean E. Sammet, editors, History of Programming Languages
Conference (HOPL-II), pages 37–52. ACM, April 1993.
Ciatto et al. (UniBo, HES-SO) 2P-Kt: LP in Kotlin WOA, 2020

2P-Kt: logic programming with objects & functions in Kotlin

  • 1.
    2P-Kt: logic programmingwith objects & functions in Kotlin Giovanni Ciatto∗ Roberta Calegari◦ Enrico Siboni† Enrico Denti‡ Andrea Omicini ∗‡ Dipartimento di Informatica – Scienza e Ingegneria (DISI) ◦Alma Mater Research Institute for Human-Centered Artificial Intelligence Alma Mater Studiorum—Universit`a di Bologna, Italy {giovanni.ciatto, roberta.calegari, enrico.denti, andrea.omicini}@unibo.it †University of Applied Sciences and Arts of Western Switzerland (HES-SO) enrico.siboni@hevs.ch 21st Workshop “From Objects to Agents” (WOA) Sept. 16, 2020, Bologna, Italy Ciatto et al. (UniBo, HES-SO) 2P-Kt: LP in Kotlin WOA, 2020 1 / 22
  • 2.
    Motivation & Context Nextin Line. . . 1 Motivation & Context 2 Kotlin DSL for Prolog 3 Behind the scenes 4 Conclusions & future works Ciatto et al. (UniBo, HES-SO) 2P-Kt: LP in Kotlin WOA, 2020 1 / 22
  • 3.
    Motivation & Context Context Newopportunities for logic-based technologies: (X)AI and MAS AI side AI is shining, brighter than ever mostly thanks to the advances in ML and sub-symbolic AI ⇒ symbolic AI is gaining momentum because of eXplanable AI (XAI) ! hybrid solution mixing logic & data-driven AI are flourishing [3] MAS side The MAS community is eager for logic-based technologies [2] to support agents’ knowledge representation, reasoning, or execution or to prove MAS properties ! despite few mature tech. exist, and even fewer are actively maintained Ciatto et al. (UniBo, HES-SO) 2P-Kt: LP in Kotlin WOA, 2020 2 / 22
  • 4.
    Motivation & Context Motivation Theproblem with logic-based technologies There is technological barrier slowing the adoption of logic programming (LP) as paradigm the exploitation of logic-based technologies while programming in the large e.g. Scala, Kotlin, Python, C# mainstream programming languages are blending several paradigms e.g. imperative, object-oriented (OOP), and functional programming (FP) except LP! mainstream platforms e.g. JVM, .NET, JS, Python are poorly interoperable with logic-based tech. Ciatto et al. (UniBo, HES-SO) 2P-Kt: LP in Kotlin WOA, 2020 3 / 22
  • 5.
    Motivation & Context Motivatingexample – SWI-Prolog’s FLI for Java Prolog [4] implementors rely on Foreign Language Interfaces (FLI) [1] (mostly targetting Java, or C) For instance, SWI-Prolog comes with a FLI for Java1: Query query = new Query("parent", new Term[] { new Atom("adam"), new Variable("X") } ); // ?- parent(adam, X). Map<String,Term> solution = query.oneSolution(); System.out.println("The child of Adam is " + solution.get("X")); → No paradigm harmonization between Prolog and the hosting language i.e. Java 1 https://jpl7.org Ciatto et al. (UniBo, HES-SO) 2P-Kt: LP in Kotlin WOA, 2020 4 / 22
  • 6.
    Motivation & Context Contributionof the paper Show that OOP, FP, and LP can be blended into a single language Propose a DSL blending Kotlin (OOP + FP) and Prolog (LP) ! DSL = domain specific language Pave the way to the creation of similar DSL in mainstream languages eg Scala Ciatto et al. (UniBo, HES-SO) 2P-Kt: LP in Kotlin WOA, 2020 5 / 22
  • 7.
    Kotlin DSL forProlog Next in Line. . . 1 Motivation & Context 2 Kotlin DSL for Prolog 3 Behind the scenes 4 Conclusions & future works Ciatto et al. (UniBo, HES-SO) 2P-Kt: LP in Kotlin WOA, 2020 5 / 22
  • 8.
    Kotlin DSL forProlog Overview Focus on. . . 1 Motivation & Context 2 Kotlin DSL for Prolog Overview Principles Functioning Advantages 3 Behind the scenes Overview 2P-Kt Kotlin 2P-Kt + Kotlin 4 Conclusions & future works Ciatto et al. (UniBo, HES-SO) 2P-Kt: LP in Kotlin WOA, 2020 5 / 22
  • 9.
    Kotlin DSL forProlog Overview Whet your appetite Our Kotlin DSL for Prolog vs. actual Prolog prolog { staticKb( rule { "ancestor"("X", "Y") `if` "parent"("X", "Y") // ancestor(X, Y) :- parent(X, Y). }, // rule { // "ancestor"("X", "Y") `if` ( // ancestor(X, Y) :- "parent"("X", "Z") and "ancestor"("Z", "Y") // parent(X, Z), ancestor(Z, Y). ) // }, // fact { "parent"("abraham", "isaac") }, // parent(abraham, isaac). fact { "parent"("isaac", "jacob") }, // parent(isaac, jacob). fact { "parent"("jacob", "joseph") } // parent(jacob, joseph). ) // // for (sol in solve("ancestor"("abraham", "D"))) // ?- ancestor(abraham, D), if (sol is Solution.Yes) // write(D), nl. println(sol.substitution["D"]) } ! try it here: https://github.com/tuProlog/prolog-dsl-example Ciatto et al. (UniBo, HES-SO) 2P-Kt: LP in Kotlin WOA, 2020 6 / 22
  • 10.
    Kotlin DSL forProlog Principles Focus on. . . 1 Motivation & Context 2 Kotlin DSL for Prolog Overview Principles Functioning Advantages 3 Behind the scenes Overview 2P-Kt Kotlin 2P-Kt + Kotlin 4 Conclusions & future works Ciatto et al. (UniBo, HES-SO) 2P-Kt: LP in Kotlin WOA, 2020 6 / 22
  • 11.
    Kotlin DSL forProlog Principles Design Principles P1 – The DSL strictly extends the hosting language → no feature of the hosting language is forbidden within the DSL P2 – The DSL is interoperable with hosting language → all features of the hosting language are allowed within the DSL → LP is harmonised with the hosting language paradigm(s) P3 – The DSL is well encapsulated within the hosting language → i.e. only usable within well-identifiable sections P4 – The DSL is as close as possible to Prolog → both syntactically & semantically Ciatto et al. (UniBo, HES-SO) 2P-Kt: LP in Kotlin WOA, 2020 7 / 22
  • 12.
    Kotlin DSL forProlog Principles Design Principles P1 – The DSL strictly extends the hosting language → no feature of the hosting language is forbidden within the DSL P2 – The DSL is interoperable with hosting language → all features of the hosting language are allowed within the DSL → LP is harmonised with the hosting language paradigm(s) P3 – The DSL is well encapsulated within the hosting language → i.e. only usable within well-identifiable sections P4 – The DSL is as close as possible to Prolog → both syntactically & semantically Ciatto et al. (UniBo, HES-SO) 2P-Kt: LP in Kotlin WOA, 2020 7 / 22
  • 13.
    Kotlin DSL forProlog Principles Design Principles P1 – The DSL strictly extends the hosting language → no feature of the hosting language is forbidden within the DSL P2 – The DSL is interoperable with hosting language → all features of the hosting language are allowed within the DSL → LP is harmonised with the hosting language paradigm(s) P3 – The DSL is well encapsulated within the hosting language → i.e. only usable within well-identifiable sections P4 – The DSL is as close as possible to Prolog → both syntactically & semantically Ciatto et al. (UniBo, HES-SO) 2P-Kt: LP in Kotlin WOA, 2020 7 / 22
  • 14.
    Kotlin DSL forProlog Principles Design Principles P1 – The DSL strictly extends the hosting language → no feature of the hosting language is forbidden within the DSL P2 – The DSL is interoperable with hosting language → all features of the hosting language are allowed within the DSL → LP is harmonised with the hosting language paradigm(s) P3 – The DSL is well encapsulated within the hosting language → i.e. only usable within well-identifiable sections P4 – The DSL is as close as possible to Prolog → both syntactically & semantically Ciatto et al. (UniBo, HES-SO) 2P-Kt: LP in Kotlin WOA, 2020 7 / 22
  • 15.
    Kotlin DSL forProlog Functioning Focus on. . . 1 Motivation & Context 2 Kotlin DSL for Prolog Overview Principles Functioning Advantages 3 Behind the scenes Overview 2P-Kt Kotlin 2P-Kt + Kotlin 4 Conclusions & future works Ciatto et al. (UniBo, HES-SO) 2P-Kt: LP in Kotlin WOA, 2020 7 / 22
  • 16.
    Kotlin DSL forProlog Functioning Functioning in the Kotlin case I The DSL is only enabled within prolog { DSL block } expressions there, Kotlin objects are automatically converted into Prolog terms Expressions of the form: "functor"( e1 , e2 , . . .) are interpreted as terms: functor(t1, t2, . . .) provided that ∀i : ei can be converted into ti Ciatto et al. (UniBo, HES-SO) 2P-Kt: LP in Kotlin WOA, 2020 8 / 22
  • 17.
    Kotlin DSL forProlog Functioning Functioning in the Kotlin case I The DSL is only enabled within prolog { DSL block } expressions there, Kotlin objects are automatically converted into Prolog terms Expressions of the form: "functor"( e1 , e2 , . . .) are interpreted as terms: functor(t1, t2, . . .) provided that ∀i : ei can be converted into ti Ciatto et al. (UniBo, HES-SO) 2P-Kt: LP in Kotlin WOA, 2020 8 / 22
  • 18.
    Kotlin DSL forProlog Functioning Functioning in the Kotlin case II Expressions of the form: rule {"head"( e1 , . . ., eN ) `if` ( eN+1 and . . . and eM ) } are interpreted as rules: head(t1, . . . , tN) :- tN+1, . . . , tM provided that ∀i : ei can be converted into ti similar syntax for facts Ciatto et al. (UniBo, HES-SO) 2P-Kt: LP in Kotlin WOA, 2020 9 / 22
  • 19.
    Kotlin DSL forProlog Functioning Functioning in the Kotlin case III Within prolog { . . . } blocks staticKb(Clause1, Clause2, ...) sets up the local static KB dynamicKb(Clause1, Clause2, ...) sets up the local dynamic KB solve(Query, Timeout) returns a lazy stream of solutions assert(Clause) appends a new clause to the local dynamic KB . . . Ciatto et al. (UniBo, HES-SO) 2P-Kt: LP in Kotlin WOA, 2020 10 / 22
  • 20.
    Kotlin DSL forProlog Functioning Kotlin to Prolog conversions Kotlin Object Type Prolog Term Type lowercase string atom uppercase string variable int, long, short, byte integer double, float real boolean atom list, array, iterable list Ciatto et al. (UniBo, HES-SO) 2P-Kt: LP in Kotlin WOA, 2020 11 / 22
  • 21.
    Kotlin DSL forProlog Advantages Focus on. . . 1 Motivation & Context 2 Kotlin DSL for Prolog Overview Principles Functioning Advantages 3 Behind the scenes Overview 2P-Kt Kotlin 2P-Kt + Kotlin 4 Conclusions & future works Ciatto et al. (UniBo, HES-SO) 2P-Kt: LP in Kotlin WOA, 2020 11 / 22
  • 22.
    Kotlin DSL forProlog Advantages Advantages of a Prolog-like DSL No further parser / code generator / compiler is needed Features of the hosting language can be exploited if Prolog falls short Support tools of the hosting language can be exploited for Prolog eg debugger, type checker, IDE, etc Bring the power of LP to mainstream programming Ciatto et al. (UniBo, HES-SO) 2P-Kt: LP in Kotlin WOA, 2020 12 / 22
  • 23.
    Behind the scenes Nextin Line. . . 1 Motivation & Context 2 Kotlin DSL for Prolog 3 Behind the scenes 4 Conclusions & future works Ciatto et al. (UniBo, HES-SO) 2P-Kt: LP in Kotlin WOA, 2020 12 / 22
  • 24.
    Behind the scenesOverview Focus on. . . 1 Motivation & Context 2 Kotlin DSL for Prolog Overview Principles Functioning Advantages 3 Behind the scenes Overview 2P-Kt Kotlin 2P-Kt + Kotlin 4 Conclusions & future works Ciatto et al. (UniBo, HES-SO) 2P-Kt: LP in Kotlin WOA, 2020 12 / 22
  • 25.
    Behind the scenesOverview Recipe for a Prolog-like DSL 1 A language with a flexible API e.g. Kotlin, Scala, Python, Groovy, etc. 2 Full fledged API for Prolog, supporting that language e.g. 2P-Kt ! see next slide 3 Leverage flexibility to hide the exploitation of the API Ciatto et al. (UniBo, HES-SO) 2P-Kt: LP in Kotlin WOA, 2020 13 / 22
  • 26.
    Behind the scenes2P-Kt Focus on. . . 1 Motivation & Context 2 Kotlin DSL for Prolog Overview Principles Functioning Advantages 3 Behind the scenes Overview 2P-Kt Kotlin 2P-Kt + Kotlin 4 Conclusions & future works Ciatto et al. (UniBo, HES-SO) 2P-Kt: LP in Kotlin WOA, 2020 13 / 22
  • 27.
    Behind the scenes2P-Kt 2P-Kt – Overview Comprehensive, modular, re-usable API covering most aspects of LP: unification Unificator mgu(Term, Term): Substitution unify(Term, Term): Term? match(Term, Term): Boolean terms Term apply(Substitution): Term freshCopy(): Term Constant value: Any Var name: String Struct functor: String args: Array<Term> Numeric Integer value: Int Real value: Double Atom value: String Truth isTrue: Boolean EmptyList List Cons head: Term tail: Term clauses & theories Clause head: Struct? body: Term RuleDirective Fact Theory assertA(Clause) assertZ(Clause) retract(Clause) retractAll(Clause) get(Clause): Sequence<Clause> 1 * resolution Solver solve(Struct,Long): Sequence<Solution> MutableSolver loadStaticKb(Theory) loadDynamicKb(Theory) Solution query: Struct Yes substitution: Unifier No Halt exception: PrologError substitutions Substitution get(Var): Term contains(Var): Boolean Unifier Fail runs on several platforms (e.g., JVM, NodeJS, Browsers, Android) more info here: https://github.com/tuProlog/2p-kt Ciatto et al. (UniBo, HES-SO) 2P-Kt: LP in Kotlin WOA, 2020 14 / 22
  • 28.
    Behind the scenesKotlin Focus on. . . 1 Motivation & Context 2 Kotlin DSL for Prolog Overview Principles Functioning Advantages 3 Behind the scenes Overview 2P-Kt Kotlin 2P-Kt + Kotlin 4 Conclusions & future works Ciatto et al. (UniBo, HES-SO) 2P-Kt: LP in Kotlin WOA, 2020 14 / 22
  • 29.
    Behind the scenesKotlin Kotlin mechanisms for DSL I We exploited 4 basic mechanisms: 1 Operator overloading interface Term { operator fun plus(other: Term): Struct = Struct.of("+", this, other) } // now one can write: val term3: Term = term1 + term2 Ciatto et al. (UniBo, HES-SO) 2P-Kt: LP in Kotlin WOA, 2020 15 / 22
  • 30.
    Behind the scenesKotlin Kotlin mechanisms for DSL II 2 Block-like lambda expressions solutions.filter({ it -> it is Solution.Yes }) .map({ it -> it.substitution["X"] }) .joinToString(" ", { it.toString() }) // ↑ can be rewritten as ↓ solutions.filter { it is Solution.Yes } .map { it.substitution["X"] } .joinToString(" ") { it.toString() } Ciatto et al. (UniBo, HES-SO) 2P-Kt: LP in Kotlin WOA, 2020 16 / 22
  • 31.
    Behind the scenesKotlin Kotlin mechanisms for DSL III 3 Extension methods fun Any.toTerm(): Term = // converts Kotlin objects into terms operator fun String.invoke(vararg args: Term): Struct = Struct.of(this, args.map { it.toTerm() }) // now one can write "member"("X", arrayOf(1 .. 3)) // member(X, [1, 2, 3]) Ciatto et al. (UniBo, HES-SO) 2P-Kt: LP in Kotlin WOA, 2020 17 / 22
  • 32.
    Behind the scenesKotlin Kotlin mechanisms for DSL IV 4 Function types/lambdas with receivers class PrologDsl { fun Any.toTerm(): Term = // ... operator fun String.invoke(vararg args: Term): Struct = // ... operator fun Any.plus(other: Any): Struct = this.toTerm() + other.toTerm() } // ↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓ type with receiver fun prolog(action: PrologDsl.() -> Any): Any = PrologDsl().action() // ↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓ lambda with receiver prolog { "f"("1") + "f"("2") } // in braces this is PrologDsl Ciatto et al. (UniBo, HES-SO) 2P-Kt: LP in Kotlin WOA, 2020 18 / 22
  • 33.
    Behind the scenesKotlin Kotlin mechanisms for DSL V This is a Kotlin-specific solution! Other languages may support DSL through different mechanisms e.g. implicits in Scala Ciatto et al. (UniBo, HES-SO) 2P-Kt: LP in Kotlin WOA, 2020 19 / 22
  • 34.
    Behind the scenes2P-Kt + Kotlin Focus on. . . 1 Motivation & Context 2 Kotlin DSL for Prolog Overview Principles Functioning Advantages 3 Behind the scenes Overview 2P-Kt Kotlin 2P-Kt + Kotlin 4 Conclusions & future works Ciatto et al. (UniBo, HES-SO) 2P-Kt: LP in Kotlin WOA, 2020 19 / 22
  • 35.
    Behind the scenes2P-Kt + Kotlin DSL design on top of 2P-Kt JVM JS Kotlin 2P-Kt Prolog DSL Scope _: Var varOf(String): Var atomOf(String): Atom structOf(String, Iterable<Term>): Struct emptyList(): EmptyList listOf(Iterable<Term>): List numOf(Double): Real numOf(Int): Integer Prolog Any.toTerm(): Term String.invoke(Any, vararg Any): Struct Substitution.get(String): Term? PrologWithUnification Any.mguWith(Any): Substitution Any.matches(Any): Boolean Any.unifyWith(Any): Term? PrologWithTheories theoryOf(vararg Clause): Theory rule(PrologWithTheories.() -> Any): Rule fact(PrologWithTheories.() -> Any): Fact and(Any, Any): Term or(Any, Any): Term PrologWithResolution solve(Any): Sequence<Solution> solverOf(...) member(Any, Any): Term is(Any, Any): Term !: Atom fail: Atom extends Onion design with incremental features Built on top of 2P-Kt and Kotlin Ciatto et al. (UniBo, HES-SO) 2P-Kt: LP in Kotlin WOA, 2020 20 / 22
  • 36.
    Conclusions & futureworks Next in Line. . . 1 Motivation & Context 2 Kotlin DSL for Prolog 3 Behind the scenes 4 Conclusions & future works Ciatto et al. (UniBo, HES-SO) 2P-Kt: LP in Kotlin WOA, 2020 20 / 22
  • 37.
    Conclusions & futureworks Conclusions & future works Summing up, in this study we. . . argued LP should be integrated in modern languages/paradigms designed an in-language, DSL-based solution prototyped an actual Kotlin-based DSL for Prolog In the future, we will try to. . . design Prolog-like DSL for other languages (e.g. Scala) design an agent-oriented (possibly BDI?) DSL extend 2P-Kt to support other sorts of inference mechanisms design a logic-based API for sub-symbolic AI Ciatto et al. (UniBo, HES-SO) 2P-Kt: LP in Kotlin WOA, 2020 21 / 22
  • 38.
    2P-Kt: logic programmingwith objects & functions in Kotlin Giovanni Ciatto∗ Roberta Calegari◦ Enrico Siboni† Enrico Denti‡ Andrea Omicini ∗‡ Dipartimento di Informatica – Scienza e Ingegneria (DISI) ◦Alma Mater Research Institute for Human-Centered Artificial Intelligence Alma Mater Studiorum—Universit`a di Bologna, Italy {giovanni.ciatto, roberta.calegari, enrico.denti, andrea.omicini}@unibo.it †University of Applied Sciences and Arts of Western Switzerland (HES-SO) enrico.siboni@hevs.ch 21st Workshop “From Objects to Agents” (WOA) Sept. 16, 2020, Bologna, Italy Ciatto et al. (UniBo, HES-SO) 2P-Kt: LP in Kotlin WOA, 2020 22 / 22
  • 39.
    Bibliography References I [1] RobertoBagnara and Manuel Carro. Foreign language interfaces for Prolog: A terse survey. ALP Newsletter, 15(2), May 2002. [2] Roberta Calegari, Giovanni Ciatto, Viviana Mascardi, and Andrea Omicini. Logic-based technologies for multi-agent systems: A systematic literature review. Autonomous Agents and Multi-Agent Systems, In press. Special Issue “Current Trends in Research on Software Agents and Agent-Based Software Development”. [3] Roberta Calegari, Giovanni Ciatto, and Andrea Omicini. On the integration of symbolic and sub-symbolic techniques for XAI: A survey. Intelligenza Artificiale, In press. [4] Alain Colmerauer and Philippe Roussel. The birth of prolog. In John A. N. Lee and Jean E. Sammet, editors, History of Programming Languages Conference (HOPL-II), pages 37–52. ACM, April 1993. Ciatto et al. (UniBo, HES-SO) 2P-Kt: LP in Kotlin WOA, 2020