129 lines
2.5 KiB
PHP
129 lines
2.5 KiB
PHP
<?php
|
|
|
|
namespace alveolata\term;
|
|
|
|
// require_once(DIR_ALVEOLATA . '/definitions.php');
|
|
require_once(DIR_ALVEOLATA . '/list/functions.php');
|
|
require_once(DIR_ALVEOLATA . '/term/interface.php');
|
|
require_once(DIR_ALVEOLATA . '/term/implementation-variable.php');
|
|
require_once(DIR_ALVEOLATA . '/term/implementation-function.php');
|
|
|
|
|
|
/**
|
|
* @param interface_term $term
|
|
* @return string
|
|
* @author Christian Fraß <frass@greenscale.de>
|
|
*/
|
|
function to_string(
|
|
interface_term $term
|
|
) : string
|
|
{
|
|
if ($term instanceof class_variable) {
|
|
$variable = $term;
|
|
return sprintf('$%s', $variable->name);
|
|
}
|
|
else if ($term instanceof class_function) {
|
|
$function = $term;
|
|
return sprintf(
|
|
'%s(%s)',
|
|
$function->head,
|
|
implode(
|
|
',',
|
|
\alveolata\list_\map(
|
|
$function->arguments,
|
|
function ($argument) {
|
|
return to_string($argument);
|
|
}
|
|
)
|
|
)
|
|
);
|
|
}
|
|
else {
|
|
throw (new \Exception('unhandled'));
|
|
}
|
|
}
|
|
|
|
|
|
/**
|
|
* @param interface_term $term1
|
|
* @param interface_term $term2
|
|
* @return bool
|
|
* @author Christian Fraß <frass@greenscale.de>
|
|
*/
|
|
function equal(
|
|
interface_term $term1,
|
|
interface_term $term2
|
|
) : bool
|
|
{
|
|
if ($term1 instanceof class_variable) {
|
|
if ($term2 instanceof class_variable) {
|
|
return ($term1->name === $term2->name);
|
|
}
|
|
else if ($term2 instanceof class_function) {
|
|
return false;
|
|
}
|
|
else {
|
|
throw (new \Exception('unhandled'));
|
|
}
|
|
}
|
|
else if ($term1 instanceof class_function) {
|
|
if ($term2 instanceof class_variable) {
|
|
return false;
|
|
}
|
|
else if ($term2 instanceof class_function) {
|
|
return (
|
|
($term1->head === $term2->head)
|
|
&&
|
|
(count($term1->arguments) === count($term2->arguments))
|
|
&&
|
|
\alveolata\list_\every(
|
|
\alveolata\list_\sequence(count($term1->arguments)),
|
|
function ($index) use (&$term1, &$term2) {
|
|
return equal($term1->arguments[$index], $term2->arguments[$index]);
|
|
}
|
|
)
|
|
);
|
|
}
|
|
else {
|
|
throw (new \Exception('unhandled'));
|
|
}
|
|
}
|
|
else {
|
|
throw (new \Exception('unhandled'));
|
|
}
|
|
}
|
|
|
|
|
|
/**
|
|
* @param interface_term $term1
|
|
* @param interface_term $term2
|
|
* @return bool
|
|
* @author Christian Fraß <frass@greenscale.de>
|
|
*/
|
|
function contains(
|
|
interface_term $term1,
|
|
interface_term $term2
|
|
) : bool
|
|
{
|
|
if (equal($term1, $term2)) {
|
|
return true;
|
|
}
|
|
else {
|
|
if ($term1 instanceof class_variable) {
|
|
return false;
|
|
}
|
|
else if ($term1 instanceof class_function) {
|
|
return \alveolata\list_\some(
|
|
$term1->arguments,
|
|
function ($argument) use (&$term2) {
|
|
return contains($argument, $term2);
|
|
}
|
|
);
|
|
}
|
|
else {
|
|
throw (new \Exception('unhandled'));
|
|
}
|
|
}
|
|
}
|
|
|
|
?>
|