-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'feature/examples' of gitlab.com:pika-lab/courses/as/pro…
…jects/sa-project-rizzato-1920 into feature/examples
- Loading branch information
Showing
10 changed files
with
177 additions
and
7 deletions.
There are no files selected for viewing
19 changes: 19 additions & 0 deletions
19
examples/src/main/kotlin/it/unibo/tuprolog/examples/core/substitution/Application.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} |
23 changes: 23 additions & 0 deletions
23
examples/src/main/kotlin/it/unibo/tuprolog/examples/core/substitution/Chain.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} |
23 changes: 23 additions & 0 deletions
23
examples/src/main/kotlin/it/unibo/tuprolog/examples/core/substitution/Composition.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} |
24 changes: 24 additions & 0 deletions
24
examples/src/main/kotlin/it/unibo/tuprolog/examples/core/substitution/Contradiction.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} |
20 changes: 20 additions & 0 deletions
20
examples/src/main/kotlin/it/unibo/tuprolog/examples/core/substitution/Creation.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} |
20 changes: 20 additions & 0 deletions
20
examples/src/main/kotlin/it/unibo/tuprolog/examples/unify/Caching.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
23 changes: 23 additions & 0 deletions
23
examples/src/main/kotlin/it/unibo/tuprolog/examples/unify/Failure.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
File renamed without changes.
19 changes: 19 additions & 0 deletions
19
examples/src/main/kotlin/it/unibo/tuprolog/examples/unify/OccursCheck.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} | ||
} |
13 changes: 6 additions & 7 deletions
13
...olog/examples/unify/UnificationExample.kt → .../unibo/tuprolog/examples/unify/Success.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |