Checkpoint

This commit is contained in:
Tibo De Peuter 2025-04-05 17:36:37 +02:00
parent 39c3af4ba5
commit da21d890fb
39 changed files with 1166 additions and 48 deletions

View file

@ -0,0 +1,20 @@
package prolog.builtins
import prolog.components.terms.Atom
/**
* Always fail.
*/
class Fail: Atom("fail")
/**
* Same as fail, but the name has a more declarative connotation.
*/
typealias False = Fail
/**
* Always succeed.
*/
class True: Atom("true")
// TODO Repeat/0

View file

@ -0,0 +1,22 @@
package prolog.builtins
import prolog.components.terms.Atom
import prolog.components.terms.Term
/**
* True when Term is a term with functor Name/Arity. If Term is a variable it is unified with a new term whose
* arguments are all different variables (such a term is called a skeleton). If Term is atomic, Arity will be unified
* with the integer 0, and Name will be unified with Term. Raises instantiation_error() if Term is unbound and
* Name/Arity is insufficiently instantiated.
*
* SWI-Prolog also supports terms with arity 0, as in a() (see
* [section 5](https://www.swi-prolog.org/pldoc/man?section=extensions)). Such terms must be processed using functor/4
* or compound_name_arity/3. The predicate functor/3 and =../2 raise a domain_error when faced with these terms.
* Without this precaution a round trip of a term with arity 0 over functor/3 would create an atom.
*
* Source: [SWI-Prolog Predicate functor/3](https://www.swi-prolog.org/pldoc/doc_for?object=functor/3)
*/
fun functor(term: Term, name: Atom, arity: Int): Boolean {
// TODO Implement
return true
}

View file

@ -0,0 +1,41 @@
package prolog.builtins
import prolog.components.terms.CompoundTerm
import prolog.components.terms.Term
import prolog.components.terms.Variable
/**
* True if [Term] is bound (i.e., not a variable) and is not compound.
* Thus, atomic acts as if defined by:
*
* atomic(Term) :-
* nonvar(Term),
* \+ compound(Term).
*/
fun atomic(term: Term): Boolean = nonvariable(term) && !compound(term)
/**
* True if [Term] is bound to a compound term.
* See also functor/3 =../2, compound_name_arity/3 and compound_name_arguments/3.
*/
fun compound(term: Term): Boolean {
val isCompound = term is CompoundTerm
val isVariableCompound = term is Variable && term.alias().isPresent && compound(term.alias().get())
return isCompound || isVariableCompound
}
/**
* True if [Term] currently is not a free variable.
*/
fun nonvariable(term: Term): Boolean = !variable(term)
/**
* True if [Term] currently is a free variable.
*/
fun variable(term: Term): Boolean {
if (term is Variable) {
return term.alias().isEmpty || term.alias().get() === term || variable(term.alias().get())
}
return false;
}