-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Introduced template terms, refactored some signatures of public API (#9)
- Loading branch information
Showing
15 changed files
with
106 additions
and
96 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,7 +6,7 @@ plugins { | |
} | ||
|
||
group = "org.klogic" | ||
version = "0.1.2" | ||
version = "0.1.3" | ||
|
||
repositories { | ||
mavenCentral() | ||
|
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
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
22 changes: 5 additions & 17 deletions
22
klogic-utils/src/main/kotlin/org/klogic/utils/terms/LogicPair.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
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
59 changes: 59 additions & 0 deletions
59
klogic-utils/src/main/kotlin/org/klogic/utils/terms/TemplateTerms.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,59 @@ | ||
package org.klogic.utils.terms | ||
|
||
import org.klogic.core.CustomTerm | ||
|
||
/** | ||
* A template for any [CustomTerm] that does not store any values - e.g., objects like [LogicBool]. | ||
*/ | ||
@Suppress("RedundantModalityModifier") | ||
abstract class EmptyTerm<Term : CustomTerm<Term>> : CustomTerm<Term> { | ||
override val subtreesToUnify: Sequence<*> = emptySequence<Any?>() | ||
|
||
override fun constructFromSubtrees(subtrees: Iterable<*>): CustomTerm<Term> = this | ||
} | ||
|
||
/** | ||
* A template fpr any [CustomTerm] that stores one value of an arbitrary type. | ||
*/ | ||
abstract class UnaryTerm<Term : CustomTerm<Term>, Value> : CustomTerm<Term> { | ||
abstract val value: Value | ||
abstract val constructor: (Value) -> UnaryTerm<Term, Value> | ||
|
||
override val subtreesToUnify: Sequence<*> | ||
get() = sequenceOf(value) | ||
|
||
override fun constructFromSubtrees(subtrees: Iterable<*>): CustomTerm<Term> { | ||
val value = subtrees.iterator().next() | ||
// Do not check other values for better performance | ||
|
||
@Suppress("UNCHECKED_CAST") | ||
return constructor(value as Value) | ||
} | ||
} | ||
|
||
/** | ||
* A template fpr any [CustomTerm] that stores two values of arbitrary types. | ||
*/ | ||
abstract class BinaryTerm<Term : CustomTerm<Term>, FirstValue, SecondValue> : CustomTerm<Term> { | ||
abstract val first: FirstValue | ||
abstract val second: SecondValue | ||
abstract val constructor: (FirstValue, SecondValue) -> BinaryTerm<Term, FirstValue, SecondValue> | ||
|
||
override val subtreesToUnify: Sequence<*> | ||
get() = sequenceOf(first, second) | ||
|
||
override fun constructFromSubtrees(subtrees: Iterable<*>): CustomTerm<Term> { | ||
val (first, second) = subtrees.takeFirstAndSecondElements() | ||
// Do not check other values for better performance | ||
|
||
@Suppress("UNCHECKED_CAST") | ||
return constructor(first as FirstValue, second as SecondValue) | ||
} | ||
|
||
private fun <T> Iterable<T>.takeFirstAndSecondElements(): Pair<T, T> { | ||
val iterator = iterator() | ||
|
||
// We use by-hand iteration here to avoid losing performance. | ||
return iterator.next() to iterator.next() | ||
} | ||
} |
Oops, something went wrong.