-
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.
fix built-in primitives with new wrappers hierarchy
- Loading branch information
Showing
14 changed files
with
106 additions
and
108 deletions.
There are no files selected for viewing
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
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
14 changes: 7 additions & 7 deletions
14
solve/src/commonMain/kotlin/it/unibo/tuprolog/solve/stdlib/primitive/FindAll.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,28 +1,28 @@ | ||
package it.unibo.tuprolog.solve.stdlib.primitive | ||
|
||
import it.unibo.tuprolog.core.List as LogicList | ||
import it.unibo.tuprolog.core.Struct | ||
import it.unibo.tuprolog.core.Substitution | ||
import it.unibo.tuprolog.core.Term | ||
import it.unibo.tuprolog.solve.ExecutionContext | ||
import it.unibo.tuprolog.solve.Solution | ||
import it.unibo.tuprolog.solve.Solve | ||
import it.unibo.tuprolog.solve.exception.error.MetaError | ||
import it.unibo.tuprolog.solve.primitive.NonBacktrackableTernaryRelation | ||
import it.unibo.tuprolog.solve.primitive.TernaryRelation | ||
import it.unibo.tuprolog.unify.Unificator.Companion.mguWith | ||
import it.unibo.tuprolog.core.List as LogicList | ||
|
||
object FindAll : NonBacktrackableTernaryRelation<ExecutionContext>("findall") { | ||
override fun Solve.Request<ExecutionContext>.computeOne(x: Term, y: Term, z: Term): Solve.Response { | ||
object FindAll : TernaryRelation.NonBacktrackable<ExecutionContext>("findall") { | ||
override fun Solve.Request<ExecutionContext>.computeOne(first: Term, second: Term, third: Term): Solve.Response { | ||
ensuringArgumentIsInstantiated(1) | ||
val solutions = solve(y as Struct).toList() | ||
val solutions = solve(second as Struct).toList() | ||
val error = solutions.asSequence().filterIsInstance<Solution.Halt>().firstOrNull() | ||
if (error != null) { | ||
return replyException(MetaError.of(context, error.exception)) | ||
} | ||
val mapped = solutions.asSequence() | ||
.filterIsInstance<Solution.Yes>() | ||
.map { x[it.substitution].freshCopy() } | ||
.map { first[it.substitution].freshCopy() } | ||
|
||
return replySuccess(z.mguWith(LogicList.from(mapped)) as Substitution.Unifier) | ||
return replySuccess(third.mguWith(LogicList.from(mapped)) as Substitution.Unifier) | ||
} | ||
} |
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
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
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
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
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
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
13 changes: 7 additions & 6 deletions
13
solve/src/commonMain/kotlin/it/unibo/tuprolog/solve/stdlib/primitive/Sleep.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,26 +1,27 @@ | ||
package it.unibo.tuprolog.solve.stdlib.primitive | ||
|
||
import it.unibo.tuprolog.core.Integer | ||
import it.unibo.tuprolog.core.Term | ||
import it.unibo.tuprolog.solve.ExecutionContext | ||
import it.unibo.tuprolog.solve.Solve | ||
import it.unibo.tuprolog.solve.currentTimeInstant | ||
import it.unibo.tuprolog.solve.exception.TimeOutException | ||
import it.unibo.tuprolog.solve.primitive.PrimitiveWrapper | ||
import it.unibo.tuprolog.solve.primitive.UnaryPredicate | ||
|
||
/** | ||
* Implements predicate `sleep(+N)` where `N` must be instantiated as an integer. | ||
* The predicate execution always succeeds, unless the resolution process is halted because of a [TimeOutException]. | ||
* Furthermore, the resolution of a `sleep(N)` sub-goal is guaranteed to require at least `N` milliseconds | ||
*/ | ||
object Sleep : PrimitiveWrapper<ExecutionContext>("sleep", 1) { | ||
override fun uncheckedImplementation(request: Solve.Request<ExecutionContext>): Sequence<Solve.Response> = | ||
object Sleep : UnaryPredicate<ExecutionContext>("sleep") { | ||
override fun Solve.Request<ExecutionContext>.computeAll(first: Term): Sequence<Solve.Response> = | ||
sequence { | ||
request.ensuringAllArgumentsAreInstantiated() | ||
ensuringAllArgumentsAreInstantiated() | ||
.ensuringArgumentIsInteger(0).let { | ||
val initialTime = currentTimeInstant() | ||
val threshold = request.arguments[0].castTo<Integer>().intValue.toLongExact() | ||
val threshold = arguments[0].castTo<Integer>().intValue.toLongExact() | ||
while (currentTimeInstant() - initialTime < threshold); | ||
yield(request.replySuccess()) | ||
yield(replySuccess()) | ||
} | ||
} | ||
} |
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
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
12 changes: 6 additions & 6 deletions
12
solve/src/commonMain/kotlin/it/unibo/tuprolog/solve/stdlib/primitive/UnifiesWith.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,13 +1,13 @@ | ||
package it.unibo.tuprolog.solve.stdlib.primitive | ||
|
||
import it.unibo.tuprolog.core.Substitution | ||
import it.unibo.tuprolog.core.Term | ||
import it.unibo.tuprolog.solve.ExecutionContext | ||
import it.unibo.tuprolog.solve.primitive.TermRelation | ||
import it.unibo.tuprolog.unify.Unificator.Companion.mguWith | ||
import it.unibo.tuprolog.solve.Solve | ||
import it.unibo.tuprolog.solve.primitive.BinaryRelation | ||
import it.unibo.tuprolog.unify.Unificator.Companion.matches | ||
|
||
/** Implementation of '='/2 predicate */ | ||
object UnifiesWith : TermRelation.WithSideEffects<ExecutionContext>("=") { | ||
override fun relationWithSideEffects(x: Term, y: Term): Substitution = | ||
x mguWith y | ||
object UnifiesWith : BinaryRelation.Predicative<ExecutionContext>("=") { | ||
override fun Solve.Request<ExecutionContext>.compute(first: Term, second: Term): Boolean = | ||
first matches second | ||
} |
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