Skip to content

Commit

Permalink
Merge branch 'feature/examples' of gitlab.com:pika-lab/courses/as/pro…
Browse files Browse the repository at this point in the history
…jects/sa-project-rizzato-1920 into feature/examples
  • Loading branch information
gciatto committed May 19, 2020
2 parents eaf7c6b + bca9d4f commit 17a8a8f
Show file tree
Hide file tree
Showing 10 changed files with 177 additions and 7 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package it.unibo.tuprolog.examples.core.substitution

import it.unibo.tuprolog.core.*

/**
*
*
* @author Lorenzo
*/
fun main() {
Scope.empty {
val term = structOf("father", varOf("X"), atomOf("isaac"))
val substitution = Substitution.Companion.of(varOf("X") to atomOf("abraham"))

val result = substitution.applyTo(term)

println(result)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package it.unibo.tuprolog.examples.core.substitution

import it.unibo.tuprolog.core.Scope
import it.unibo.tuprolog.core.Substitution

/**
*
*
* @author Lorenzo
*/
fun main() {
Scope.empty {
val substitution = Substitution.of(
varOf("X") to varOf("Y"),
varOf("Y") to varOf("Z")
)

val originalZ = substitution.getOriginal(varOf("Z"))

println(substitution)
println(originalZ)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package it.unibo.tuprolog.examples.core.substitution

import it.unibo.tuprolog.core.Scope
import it.unibo.tuprolog.core.Substitution

/**
*
*
* @author Lorenzo
*/
fun main() {
Scope.empty {
val term = structOf("father", varOf("X"), varOf("Y"))

val sub1 = Substitution.of(varOf("X"), atomOf("abraham"))
val sub2 = Substitution.of(varOf("Y"), atomOf("isaac"))

val substitution = sub1 + sub2

val result = substitution.applyTo(term)
println(result)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package it.unibo.tuprolog.examples.core.substitution

import it.unibo.tuprolog.core.Scope
import it.unibo.tuprolog.core.Substitution

/**
*
*
* @author Lorenzo
*/
fun main() {
Scope.empty {
val term = structOf("father", varOf("X"), atomOf("isaac"))

val sub1 = Substitution.of(varOf("X"), atomOf("abraham"))
val sub2 = Substitution.of(varOf("X"), atomOf("nahor"))

val substitution = sub1 + sub2 // contradiction!
val result = substitution.applyTo(term) // father(X_0, isaac) (substitution could not be performed)

println(substitution)
println(result)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package it.unibo.tuprolog.examples.core.substitution

import it.unibo.tuprolog.core.Scope
import it.unibo.tuprolog.core.Substitution

/**
*
*
* @author Lorenzo
*/
fun main() {
Scope.empty {
val substitution = Substitution.of(
varOf("X") to atomOf("abraham"),
varOf("Y") to atomOf("isaac")
)

println(substitution)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package it.unibo.tuprolog.examples.unify

import it.unibo.tuprolog.core.*
import it.unibo.tuprolog.unify.Unificator

fun main() {
val unificator = Unificator.default
val cached = Unificator.cached(unificator, capacity = 5)

val term = Struct.of("father", Atom.of("abraham"), Atom.of("isaac"))
val template = Struct.of("father", Var.of("X"), Atom.of("isaac"))

val substitution: Substitution = cached.mgu(term, template)
val match: Boolean = cached.match(term, template)
val unified: Term? = cached.unify(term, template)

println(substitution) // {X_0=abraham}
println(match) // true
println(unified) // father(abraham, isaac)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package it.unibo.tuprolog.examples.unify

import it.unibo.tuprolog.core.Atom
import it.unibo.tuprolog.core.Struct
import it.unibo.tuprolog.core.Substitution
import it.unibo.tuprolog.core.Term
import it.unibo.tuprolog.unify.Unificator

fun main() {
val unificator = Unificator.default

val term = Struct.of("father", Atom.of("abraham"), Atom.of("isaac"))
val template = Struct.of("father", Atom.of("isaac"), Atom.of("abraham"))

val substitution: Substitution = unificator.mgu(term, template)
val match: Boolean = unificator.match(term, template)
val unified: Term? = unificator.unify(term, template)

println(substitution is Substitution.Fail) // true
println(substitution.isFailed) // true
println(match) // false
println(unified) // null
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package it.unibo.tuprolog.examples.unify

import it.unibo.tuprolog.core.Scope
import it.unibo.tuprolog.dsl.unify.prolog
import it.unibo.tuprolog.unify.Unificator

fun main() {
prolog {
Scope.empty {
val term = "g"("X", "Y")
val otherTerm = "g"("f"("X"), "a")

val unificator = Unificator.default

val mgu = unificator.mgu(term, otherTerm, occurCheckEnabled = false)
println(mgu) // {X_0=f(X_0), Y_1=a} => WRONG
}
}
}
Original file line number Diff line number Diff line change
@@ -1,20 +1,19 @@
package it.unibo.tuprolog.examples.unify

import it.unibo.tuprolog.core.Atom
import it.unibo.tuprolog.core.Struct
import it.unibo.tuprolog.core.Var
import it.unibo.tuprolog.core.*
import it.unibo.tuprolog.unify.Unificator

fun main() {
val unificator = Unificator.default

val term = Struct.of("father", Atom.of("abraham"), Atom.of("isaac"))
val template = Struct.of("father", Var.of("X"), Atom.of("isaac"))

val substitution = unificator.mgu(term, template)
val match = unificator.match(term, template)
val unified = unificator.unify(term, template)
val substitution: Substitution = unificator.mgu(term, template)
val match: Boolean = unificator.match(term, template)
val unified: Term? = unificator.unify(term, template)

println(substitution) // {X_0=abraham}
println(match) // true
println(unified) // father(abraham, isaac)
println(unified) // father(abraham, isaac)
}

0 comments on commit 17a8a8f

Please sign in to comment.