revamp evaluable functors, add missing evaluable functors

This commit is contained in:
Mark Thom
2019-05-12 20:14:00 -04:00
parent f988b67403
commit f344150322
19 changed files with 1083 additions and 308 deletions

View File

@@ -1,6 +1,6 @@
[package]
name = "scryer-prolog"
version = "0.8.81"
version = "0.8.82"
authors = ["Mark Thom <markjordanthom@gmail.com>"]
repository = "https://github.com/mthom/scryer-prolog"
description = "A modern Prolog implementation written mostly in Rust."
@@ -13,11 +13,11 @@ default = ["readline_rs_compat"]
cfg-if = "0.1.7"
downcast = "0.10.0"
indexmap = "1.0.2"
num = "0.2"
ordered-float = "0.5.0"
prolog_parser = "0.8.26"
prolog_parser = "0.8.27"
readline_rs_compat = { version = "0.1.9", optional = true }
ref_thread_local = "0.0.0"
rug = "1.4.0"
[dependencies.termion]
version = "1.4.0"

View File

@@ -133,8 +133,12 @@ to my knowledge is not currently the case.
The following predicates are built-in to Scryer.
* Arithmetic support:
* `is/2` works for `(+)/2`, `(-)/{1,2}`, `(*)/2`, `(//)/2`, `(**)/2`, `(div)/2`, `(/)/2`, `(rdiv)/2`,
`(xor)/2`, `(rem)/2`, `(mod)/2`, `(/\)/2`, `(\/)/2`, `(>>)/2`, `(<<)/2`, `abs/1`.
* `is/2` works for `(+)/2`, `(-)/{1,2}`, `(*)/2`, `(//)/2`,
`(**)/2`, `(^)/2`, `(div)/2`, `(/)/2`, `(rdiv)/2`, `(xor)/2`,
`(rem)/2`, `(mod)/2`, `(/\)/2`, `(\/)/2`, `(>>)/2`,`(<<)/2`,
`(\)/1`, `abs/1`, `sin/1`, `cos/1`, `tan/1`, `asin/1`, `acos/1`,
`atan/1`, `atan2/2`, `log/1`, `exp/1`, `sqrt/1`, `float/1`,
`truncate/1`, `round/1`, `floor/1`, `ceiling/1`
* Comparison operators: `>`, `<`, `=<`, `>=`, `=:=`, `=\=`.
* `(:)/2`
* `(@>)/2`

View File

@@ -6,10 +6,18 @@ use prolog::forms::*;
use prolog::instructions::*;
use prolog::iterators::*;
use prolog::machine::machine_errors::*;
use prolog::machine::machine_indices::*;
use prolog::ordered_float::*;
use prolog::rug::{Integer, Rational};
use prolog::rug::ops::PowAssign;
use std::cell::Cell;
use std::cmp::{min, max};
use std::cmp::{Ordering, min, max};
use std::f64;
use std::num::FpCategory;
use std::ops::{Add, Sub, Div, Mul, Neg};
use std::rc::Rc;
use std::vec::Vec;
@@ -27,17 +35,19 @@ impl<'a> ArithInstructionIterator<'a> {
fn new(term: &'a Term) -> Result<Self, ArithmeticError> {
let state = match term {
&Term::AnonVar =>
return Err(ArithmeticError::InvalidTerm),
return Err(ArithmeticError::UninstantiatedVar),
&Term::Clause(ref cell, ref name, ref terms, ref fixity) =>
match ClauseType::from(name.clone(), terms.len(), fixity.clone()) {
ct @ ClauseType::Named(..) | ct @ ClauseType::Op(..) =>
Ok(TermIterState::Clause(Level::Shallow, 0, cell, ct, terms)),
_ => Err(ArithmeticError::InvalidOp)
_ => Err(ArithmeticError::NonEvaluableFunctor(Constant::Atom(name.clone(),
fixity.clone()),
terms.len()))
}?,
&Term::Constant(ref cell, ref cons) =>
TermIterState::Constant(Level::Shallow, cell, cons),
&Term::Cons(_, _, _) =>
return Err(ArithmeticError::InvalidTerm),
return Err(ArithmeticError::NonEvaluableFunctor(atom!("'.'"), 2)),
&Term::Var(ref cell, ref var) =>
TermIterState::Var(Level::Shallow, cell, var.clone())
};
@@ -61,7 +71,7 @@ impl<'a> Iterator for ArithInstructionIterator<'a> {
TermIterState::AnonVar(_) =>
return Some(Err(ArithmeticError::UninstantiatedVar)),
TermIterState::Clause(lvl, child_num, cell, ct, subterms) => {
let arity = subterms.len();
let arity = subterms.len();
if child_num == arity {
return Some(Ok(ArithTermRef::Op(ct.name(), arity)));
@@ -75,7 +85,7 @@ impl<'a> Iterator for ArithInstructionIterator<'a> {
TermIterState::Var(_, cell, var) =>
return Some(Ok(ArithTermRef::Var(cell, var.clone()))),
_ =>
return Some(Err(ArithmeticError::InvalidTerm))
return Some(Err(ArithmeticError::NonEvaluableFunctor(atom!("'.'"), 2)))
};
}
@@ -115,7 +125,23 @@ impl<'a> ArithmeticEvaluator<'a>
match name.as_str() {
"abs" => Ok(ArithmeticInstruction::Abs(a1, t)),
"-" => Ok(ArithmeticInstruction::Neg(a1, t)),
_ => Err(ArithmeticError::InvalidOp)
"+" => Ok(ArithmeticInstruction::Plus(a1, t)),
"cos" => Ok(ArithmeticInstruction::Cos(a1, t)),
"sin" => Ok(ArithmeticInstruction::Sin(a1, t)),
"tan" => Ok(ArithmeticInstruction::Tan(a1, t)),
"log" => Ok(ArithmeticInstruction::Log(a1, t)),
"exp" => Ok(ArithmeticInstruction::Exp(a1, t)),
"sqrt" => Ok(ArithmeticInstruction::Sqrt(a1, t)),
"acos" => Ok(ArithmeticInstruction::ACos(a1, t)),
"asin" => Ok(ArithmeticInstruction::ASin(a1, t)),
"atan" => Ok(ArithmeticInstruction::ATan(a1, t)),
"float" => Ok(ArithmeticInstruction::Float(a1, t)),
"truncate" => Ok(ArithmeticInstruction::Truncate(a1, t)),
"round" => Ok(ArithmeticInstruction::Round(a1, t)),
"ceiling" => Ok(ArithmeticInstruction::Ceiling(a1, t)),
"floor" => Ok(ArithmeticInstruction::Floor(a1, t)),
"\\" => Ok(ArithmeticInstruction::BitwiseComplement(a1, t)),
_ => Err(ArithmeticError::NonEvaluableFunctor(Constant::Atom(name, None), 1))
}
}
@@ -128,7 +154,8 @@ impl<'a> ArithmeticEvaluator<'a>
"/" => Ok(ArithmeticInstruction::Div(a1, a2, t)),
"//" => Ok(ArithmeticInstruction::IDiv(a1, a2, t)),
"max" => Ok(ArithmeticInstruction::Max(a1, a2, t)),
"div" => Ok(ArithmeticInstruction::FIDiv(a1, a2, t)),
"min" => Ok(ArithmeticInstruction::Min(a1, a2, t)),
"div" => Ok(ArithmeticInstruction::IntFloorDiv(a1, a2, t)),
"rdiv" => Ok(ArithmeticInstruction::RDiv(a1, a2, t)),
"*" => Ok(ArithmeticInstruction::Mul(a1, a2, t)),
"**" => Ok(ArithmeticInstruction::Pow(a1, a2, t)),
@@ -140,7 +167,8 @@ impl<'a> ArithmeticEvaluator<'a>
"xor" => Ok(ArithmeticInstruction::Xor(a1, a2, t)),
"mod" => Ok(ArithmeticInstruction::Mod(a1, a2, t)),
"rem" => Ok(ArithmeticInstruction::Rem(a1, a2, t)),
_ => Err(ArithmeticError::InvalidOp)
"atan2" => Ok(ArithmeticInstruction::ATan2(a1, a2, t)),
_ => Err(ArithmeticError::NonEvaluableFunctor(Constant::Atom(name, None), 2))
}
}
@@ -193,16 +221,20 @@ impl<'a> ArithmeticEvaluator<'a>
Self::get_binary_instr(name, a1, a2, ninterm)
},
_ => Err(ArithmeticError::InvalidOp)
_ => Err(ArithmeticError::NonEvaluableFunctor(Constant::Atom(name, None), arity))
}
}
fn push_constant(&mut self, c: &Constant) -> Result<(), ArithmeticError> {
match c {
&Constant::Number(ref n) =>
self.interm.push(ArithmeticTerm::Number(n.clone())),
&Constant::Integer(ref n) =>
self.interm.push(ArithmeticTerm::Number(Number::Integer(n.clone()))),
&Constant::Float(ref n) =>
self.interm.push(ArithmeticTerm::Number(Number::Float(n.clone()))),
&Constant::Rational(ref n) =>
self.interm.push(ArithmeticTerm::Number(Number::Rational(n.clone()))),
_ =>
return Err(ArithmeticError::InvalidAtom),
return Err(ArithmeticError::NonEvaluableFunctor(c.clone(), 0))
}
Ok(())
@@ -240,3 +272,238 @@ impl<'a> ArithmeticEvaluator<'a>
}
}
// integer division rounding function -- 9.1.3.1.
pub fn rnd_i(n: Number) -> Integer {
match n {
Number::Integer(n) => n,
Number::Float(OrderedFloat(f)) =>
Integer::from_f64(f.floor()).unwrap_or_else(|| Integer::from(0)),
Number::Rational(r) => r.fract_floor(Integer::new()).1
}
}
// floating point rounding function -- 9.1.4.1.
pub fn rnd_f(n: Number) -> f64 {
match n {
Number::Integer(n) => n.to_f64(),
Number::Float(OrderedFloat(f)) => f,
Number::Rational(r) => r.to_f64()
}
}
// floating point result function -- 9.1.4.2.
pub fn result_f<Round>(n: Number, round: Round) -> Result<f64, EvalError>
where Round: Fn(Number) -> f64
{
let f = rnd_f(n);
match f.classify() {
FpCategory::Normal | FpCategory::Zero =>
Ok(round(Number::Float(OrderedFloat(f)))),
FpCategory::Infinite => {
let f = round(Number::Float(OrderedFloat(f)));
if OrderedFloat(f) == OrderedFloat(f64::MAX) {
Ok(f)
} else {
Err(EvalError::FloatOverflow)
}
},
FpCategory::Nan => Err(EvalError::Undefined),
_ => Ok(round(Number::Float(OrderedFloat(f))))
}
}
fn float_i_to_f(n: Integer) -> Result<f64, EvalError> {
result_f(Number::Integer(n), rnd_f)
}
fn float_r_to_f(r: Rational) -> Result<f64, EvalError> {
result_f(Number::Rational(r), rnd_f)
}
fn add_f(f1: f64, f2: f64) -> Result<OrderedFloat<f64>, EvalError> {
Ok(OrderedFloat(result_f(Number::Float(OrderedFloat(f1 + f2)), rnd_f)?))
}
fn mul_f(f1: f64, f2: f64) -> Result<OrderedFloat<f64>, EvalError> {
Ok(OrderedFloat(result_f(Number::Float(OrderedFloat(f1 * f2)), rnd_f)?))
}
fn div_f(f1: f64, f2: f64) -> Result<OrderedFloat<f64>, EvalError> {
if FpCategory::Zero == f2.classify() {
Err(EvalError::ZeroDivisor)
} else {
Ok(OrderedFloat(result_f(Number::Float(OrderedFloat(f1 / f2)), rnd_f)?))
}
}
impl Add<Number> for Number {
type Output = Result<Number, EvalError>;
fn add(self, rhs: Number) -> Self::Output {
match (self, rhs) {
(Number::Integer(n1), Number::Integer(n2)) =>
Ok(Number::Integer(n1 + n2)), // add_i
(Number::Integer(n1), Number::Float(OrderedFloat(n2)))
| (Number::Float(OrderedFloat(n2)), Number::Integer(n1)) =>
Ok(Number::Float(add_f(float_i_to_f(n1)?, n2)?)),
(Number::Integer(n1), Number::Rational(n2))
| (Number::Rational(n2), Number::Integer(n1)) =>
Ok(Number::Rational(Rational::from(n1) + n2)),
(Number::Rational(n1), Number::Float(OrderedFloat(n2)))
| (Number::Float(OrderedFloat(n2)), Number::Rational(n1)) =>
Ok(Number::Float(add_f(float_r_to_f(n1)?, n2)?)),
(Number::Float(OrderedFloat(f1)), Number::Float(OrderedFloat(f2))) =>
Ok(Number::Float(add_f(f1, f2)?)),
(Number::Rational(r1), Number::Rational(r2)) =>
Ok(Number::Rational(r1 + r2))
}
}
}
impl Neg for Number {
type Output = Number;
fn neg(self) -> Self::Output {
match self {
Number::Integer(n) => Number::Integer(-n),
Number::Float(OrderedFloat(f)) => Number::Float(OrderedFloat(-f)),
Number::Rational(r) => Number::Rational(-r)
}
}
}
impl Sub<Number> for Number {
type Output = Result<Number, EvalError>;
fn sub(self, rhs: Number) -> Self::Output {
self.add(-rhs)
}
}
impl Mul<Number> for Number {
type Output = Result<Number, EvalError>;
fn mul(self, rhs: Number) -> Self::Output {
match (self, rhs) {
(Number::Integer(n1), Number::Integer(n2)) =>
Ok(Number::Integer(n1 * n2)), // mul_i
(Number::Integer(n1), Number::Float(OrderedFloat(n2)))
| (Number::Float(OrderedFloat(n2)), Number::Integer(n1)) =>
Ok(Number::Float(mul_f(float_i_to_f(n1)?, n2)?)),
(Number::Integer(n1), Number::Rational(n2))
| (Number::Rational(n2), Number::Integer(n1)) =>
Ok(Number::Rational(Rational::from(n1) * n2)),
(Number::Rational(n1), Number::Float(OrderedFloat(n2)))
| (Number::Float(OrderedFloat(n2)), Number::Rational(n1)) =>
Ok(Number::Float(mul_f(float_r_to_f(n1)?, n2)?)),
(Number::Float(OrderedFloat(f1)), Number::Float(OrderedFloat(f2))) =>
Ok(Number::Float(mul_f(f1, f2)?)),
(Number::Rational(r1), Number::Rational(r2)) =>
Ok(Number::Rational(r1 * r2))
}
}
}
impl Div<Number> for Number {
type Output = Result<Number, EvalError>;
fn div(self, rhs: Number) -> Self::Output {
match (self, rhs) {
(Number::Integer(n1), Number::Integer(n2)) =>
Ok(Number::Float(div_f(float_i_to_f(n1)?, float_i_to_f(n2)?)?)),
(Number::Integer(n1), Number::Float(OrderedFloat(n2))) =>
Ok(Number::Float(div_f(float_i_to_f(n1)?, n2)?)),
(Number::Float(OrderedFloat(n2)), Number::Integer(n1)) =>
Ok(Number::Float(div_f(n2, float_i_to_f(n1)?)?)),
(Number::Integer(n1), Number::Rational(n2)) =>
Ok(Number::Float(div_f(float_i_to_f(n1)?, float_r_to_f(n2)?)?)),
(Number::Rational(n2), Number::Integer(n1)) =>
Ok(Number::Float(div_f(float_r_to_f(n2)?, float_i_to_f(n1)?)?)),
(Number::Rational(n1), Number::Float(OrderedFloat(n2))) =>
Ok(Number::Float(div_f(float_r_to_f(n1)?, n2)?)),
(Number::Float(OrderedFloat(n2)), Number::Rational(n1)) =>
Ok(Number::Float(div_f(n2, float_r_to_f(n1)?)?)),
(Number::Float(OrderedFloat(f1)), Number::Float(OrderedFloat(f2))) =>
Ok(Number::Float(div_f(f1, f2)?)),
(Number::Rational(r1), Number::Rational(r2)) =>
Ok(Number::Float(div_f(float_r_to_f(r1)?, float_r_to_f(r2)?)?))
}
}
}
impl PartialOrd for Number {
fn partial_cmp(&self, rhs: &Number) -> Option<Ordering> {
match (self, rhs) {
(&Number::Integer(ref n1), &Number::Integer(ref n2)) =>
Some(n1.cmp(n2)),
(&Number::Integer(_), Number::Float(_)) =>
Some(Ordering::Greater),
(&Number::Float(_), &Number::Integer(_)) =>
Some(Ordering::Less),
(&Number::Integer(_), &Number::Rational(_)) =>
Some(Ordering::Greater),
(&Number::Rational(_), &Number::Integer(_)) =>
Some(Ordering::Less),
(&Number::Rational(_), Number::Float(_)) =>
Some(Ordering::Greater),
(&Number::Float(_), &Number::Rational(_)) =>
Some(Ordering::Less),
(&Number::Float(f1), &Number::Float(f2)) =>
Some(f1.cmp(&f2)),
(&Number::Rational(ref r1), &Number::Rational(ref r2)) =>
Some(r1.cmp(&r2))
}
}
}
impl Ord for Number {
fn cmp(&self, rhs: &Number) -> Ordering {
match (self, rhs) {
(&Number::Integer(ref n1), &Number::Integer(ref n2)) =>
n1.cmp(n2),
(&Number::Integer(_), Number::Float(_)) =>
Ordering::Greater,
(&Number::Float(_), &Number::Integer(_)) =>
Ordering::Less,
(&Number::Integer(_), &Number::Rational(_)) =>
Ordering::Greater,
(&Number::Rational(_), &Number::Integer(_)) =>
Ordering::Less,
(&Number::Rational(_), Number::Float(_)) =>
Ordering::Greater,
(&Number::Float(_), &Number::Rational(_)) =>
Ordering::Less,
(&Number::Float(f1), &Number::Float(f2)) =>
f1.cmp(&f2),
(&Number::Rational(ref r1), &Number::Rational(ref r2)) =>
r1.cmp(&r2)
}
}
}
// Computes n ^ power. Ignores the sign of power.
pub fn binary_pow(mut n: Integer, power: Integer) -> Integer
{
let one = Integer::from(1);
let mut power = power.abs();
if power == Integer::from(0) {
return Integer::from(1);
}
let mut oddand = Integer::from(1);
while power > one {
if power.is_odd() {
oddand *= &n;
}
n.pow_assign(2);
power >>= 1;
}
n * oddand
}

View File

@@ -1,12 +1,13 @@
use prolog_parser::ast::*;
use prolog::forms::Number;
use prolog::machine::machine_indices::*;
use ref_thread_local::RefThreadLocal;
use std::collections::BTreeMap;
#[derive(Clone, Copy, Eq, PartialEq, Ord, PartialOrd)]
#[derive(Clone, Copy, Eq, PartialEq)]
pub enum CompareNumberQT {
GreaterThan,
LessThan,
@@ -29,7 +30,7 @@ impl CompareNumberQT {
}
}
#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
#[derive(Clone, Copy, PartialEq, Eq)]
pub enum CompareTermQT {
LessThan,
LessThanOrEqual,
@@ -48,7 +49,7 @@ impl CompareTermQT {
}
}
#[derive(Clone, PartialEq, Eq, PartialOrd, Ord)]
#[derive(Clone, PartialEq, Eq)]
pub enum ArithmeticTerm {
Reg(RegType),
Interm(usize),
@@ -65,7 +66,7 @@ impl ArithmeticTerm {
}
}
#[derive(Clone, Eq, Ord, PartialOrd, PartialEq)]
#[derive(Clone, Eq, PartialEq)]
pub enum InlinedClauseType {
CompareNumber(CompareNumberQT, ArithmeticTerm, ArithmeticTerm),
IsAtom(RegType),
@@ -147,7 +148,7 @@ impl InlinedClauseType {
}
}
#[derive(Copy, Clone, Eq, PartialEq, Ord, PartialOrd)]
#[derive(Copy, Clone, Eq, PartialEq)]
pub enum SystemClauseType {
AbolishClause,
AbolishModuleClause,
@@ -416,7 +417,7 @@ impl SystemClauseType {
}
}
#[derive(Clone, Eq, PartialEq, Ord, PartialOrd)]
#[derive(Clone, Eq, PartialEq)]
pub enum BuiltInClauseType {
AcyclicTerm,
Arg,
@@ -436,7 +437,7 @@ pub enum BuiltInClauseType {
Sort,
}
#[derive(Clone, PartialEq, Eq, Ord, PartialOrd)]
#[derive(Clone, PartialEq, Eq)]
pub enum ClauseType {
BuiltIn(BuiltInClauseType),
CallN,

View File

@@ -305,13 +305,13 @@ impl<'a, TermMarker: Allocator<'a>> CodeGenerator<TermMarker>
match ct {
&InlinedClauseType::CompareNumber(cmp, ..) => {
if let &Term::Var(ref vr, ref name) = terms[0].as_ref() {
self.mark_non_callable(name.clone(), 2, term_loc, vr, code);
self.mark_non_callable(name.clone(), 2, term_loc, vr, code);
}
if let &Term::Var(ref vr, ref name) = terms[1].as_ref() {
self.mark_non_callable(name.clone(), 2, term_loc, vr, code);
self.mark_non_callable(name.clone(), 2, term_loc, vr, code);
}
let (mut lcode, at_1) = self.call_arith_eval(terms[0].as_ref(), 1)?;
let (mut rcode, at_2) = self.call_arith_eval(terms[1].as_ref(), 2)?;
@@ -365,7 +365,7 @@ impl<'a, TermMarker: Allocator<'a>> CodeGenerator<TermMarker>
},
&InlinedClauseType::IsRational(..) =>
match terms[0].as_ref() {
&Term::Constant(_, Constant::Number(Number::Rational(_))) => {
&Term::Constant(_, Constant::Rational(_)) => {
code.push(succeed!());
},
&Term::Var(ref vr, ref name) => {
@@ -378,7 +378,7 @@ impl<'a, TermMarker: Allocator<'a>> CodeGenerator<TermMarker>
},
&InlinedClauseType::IsFloat(..) =>
match terms[0].as_ref() {
&Term::Constant(_, Constant::Number(Number::Float(_))) => {
&Term::Constant(_, Constant::Float(_)) => {
code.push(succeed!());
},
&Term::Var(ref vr, ref name) => {
@@ -418,7 +418,7 @@ impl<'a, TermMarker: Allocator<'a>> CodeGenerator<TermMarker>
&InlinedClauseType::IsInteger(..) =>
match terms[0].as_ref() {
&Term::Constant(_, Constant::CharCode(_))
| &Term::Constant(_, Constant::Number(Number::Integer(_))) => {
| &Term::Constant(_, Constant::Integer(_)) => {
code.push(succeed!());
},
&Term::Var(ref vr, ref name) => {
@@ -486,7 +486,25 @@ impl<'a, TermMarker: Allocator<'a>> CodeGenerator<TermMarker>
code.push(is_call!(temp_v!(1), at.unwrap_or(interm!(1))))
}
},
&Term::Constant(_, ref c @ Constant::Number(_)) => {
&Term::Constant(_, ref c @ Constant::Integer(_)) => {
code.push(Line::Query(put_constant!(Level::Shallow, c.clone(), temp_v!(1))));
if use_default_call_policy {
code.push(is_call_by_default!(temp_v!(1), at.unwrap_or(interm!(1))))
} else {
code.push(is_call!(temp_v!(1), at.unwrap_or(interm!(1))))
}
},
&Term::Constant(_, ref c @ Constant::Float(_)) => {
code.push(Line::Query(put_constant!(Level::Shallow, c.clone(), temp_v!(1))));
if use_default_call_policy {
code.push(is_call_by_default!(temp_v!(1), at.unwrap_or(interm!(1))))
} else {
code.push(is_call!(temp_v!(1), at.unwrap_or(interm!(1))))
}
},
&Term::Constant(_, ref c @ Constant::Rational(_)) => {
code.push(Line::Query(put_constant!(Level::Shallow, c.clone(), temp_v!(1))));
if use_default_call_policy {

View File

@@ -5,6 +5,8 @@ use prolog_parser::tabled_rc::*;
use prolog::clause_types::*;
use prolog::machine::machine_errors::*;
use prolog::machine::machine_indices::*;
use prolog::ordered_float::OrderedFloat;
use prolog::rug::{Integer, Rational};
use std::cell::Cell;
use std::collections::{HashMap, VecDeque};
@@ -288,3 +290,62 @@ pub struct Module {
pub user_goal_expansions: (Predicate, VecDeque<TopLevel>), // same for goal_expansions.
pub inserted_expansions: bool // has the module been successfully inserted into toplevel??
}
#[derive(Clone, PartialEq, Eq)]
pub enum Number {
Float(OrderedFloat<f64>),
Integer(Integer),
Rational(Rational)
}
impl Default for Number {
fn default() -> Self {
Number::Float(OrderedFloat(0f64))
}
}
impl Number {
pub fn to_constant(self) -> Constant {
match self {
Number::Integer(n) => Constant::Integer(n),
Number::Float(f) => Constant::Float(f),
Number::Rational(r) => Constant::Rational(r)
}
}
#[inline]
pub fn is_positive(&self) -> bool {
match self {
&Number::Integer(ref n) => n > &0,
&Number::Float(OrderedFloat(f)) => f.is_sign_positive(),
&Number::Rational(ref r) => r > &0
}
}
#[inline]
pub fn is_negative(&self) -> bool {
match self {
&Number::Integer(ref n) => n < &0,
&Number::Float(OrderedFloat(f)) => f.is_sign_negative(),
&Number::Rational(ref r) => r < &0
}
}
#[inline]
pub fn is_zero(&self) -> bool {
match self {
&Number::Integer(ref n) => n == &0,
&Number::Float(f) => f == OrderedFloat(0f64),
&Number::Rational(ref r) => r == &0
}
}
#[inline]
pub fn abs(self) -> Self {
match self {
Number::Integer(n) => Number::Integer(n.abs()),
Number::Float(f) => Number::Float(OrderedFloat(f.abs())),
Number::Rational(r) => Number::Rational(r.abs())
}
}
}

View File

@@ -2,12 +2,12 @@ use prolog_parser::ast::*;
use prolog_parser::string_list::*;
use prolog::clause_types::*;
use prolog::forms::{fetch_atom_op_spec, fetch_op_spec};
use prolog::forms::*;
use prolog::heap_iter::*;
use prolog::machine::machine_indices::*;
use prolog::machine::machine_state::*;
use prolog::num::*;
use prolog::ordered_float::OrderedFloat;
use prolog::rug::{Integer};
use std::cell::Cell;
use std::collections::{HashMap, HashSet};
@@ -112,8 +112,12 @@ impl<'a> HCPreOrderIterator<'a> {
_ =>
return false
},
Addr::Con(Constant::Number(n)) =>
return property_check(Constant::Number(n)),
Addr::Con(Constant::Integer(n)) =>
return property_check(Constant::Integer(n)),
Addr::Con(Constant::Float(n)) =>
return property_check(Constant::Float(n)),
Addr::Con(Constant::Rational(n)) =>
return property_check(Constant::Rational(n)),
_ =>
return false
}
@@ -253,7 +257,9 @@ fn negated_op_needs_bracketing(iter: &HCPreOrderIterator, op: &Option<DirectedOp
if let &Some(ref op) = op {
op.is_negative_sign() && iter.leftmost_leaf_has_property(|c| {
match c {
Constant::Number(n) => n.is_positive(),
Constant::Integer(n) => n > 0,
Constant::Float(f) => f > OrderedFloat(0f64),
Constant::Rational(r) => r > 0,
_ => false
}
})
@@ -263,20 +269,21 @@ fn negated_op_needs_bracketing(iter: &HCPreOrderIterator, op: &Option<DirectedOp
}
impl MachineState {
pub fn numbervar(&self, offset: &BigInt, addr: Addr) -> Option<Var> {
pub fn numbervar(&self, offset: &Integer, addr: Addr) -> Option<Var> {
static CHAR_CODES: [char; 26] = ['A','B','C','D','E','F','G','H','I','J',
'K','L','M','N','O','P','Q','R','S','T',
'U','V','W','X','Y','Z'];
match self.store(self.deref(addr)) {
Addr::Con(Constant::Number(Number::Integer(ref n)))
if !n.is_negative() => {
let n = offset + n.as_ref();
Addr::Con(Constant::Integer(ref n))
if n >= &0 => {
let n = Integer::from(offset + n);
let i = n.mod_floor(&BigInt::from(26)).to_usize().unwrap();
let j = n.div_floor(&BigInt::from(26));
Some(if j.is_zero() {
let i = n.mod_u(26) as usize;
let j = n.div_rem_floor(Integer::from(26));
let j = <(Integer, Integer)>::from(j).1;
Some(if j == 0 {
CHAR_CODES[i].to_string()
} else {
format!("{}{}", CHAR_CODES[i], j)
@@ -300,7 +307,7 @@ pub struct HCPrinter<'a, Outputter> {
last_item_idx: usize,
cyclic_terms: HashMap<Addr, usize>,
pub(crate) var_names: HashMap<Addr, String>,
pub(crate) numbervars_offset: BigInt,
pub(crate) numbervars_offset: Integer,
pub(crate) numbervars: bool,
pub(crate) quoted: bool,
pub(crate) ignore_ops: bool
@@ -389,7 +396,7 @@ fn non_quoted_token<Iter: Iterator<Item=char>>(mut iter: Iter) -> bool {
} else if c == '{' {
(iter.next() == Some('}') && iter.next().is_none())
} else if solo_char!(c) {
false
!(c == ')' || c == '}' || c == ']' || c == ',' || c == '%' || c == '|')
} else {
false
}
@@ -411,7 +418,7 @@ impl<'a, Outputter: HCValueOutputter> HCPrinter<'a, Outputter>
printed_vars: HashSet::new(),
last_item_idx: 0,
numbervars: false,
numbervars_offset: BigInt::zero(),
numbervars_offset: Integer::from(0),
quoted: false,
ignore_ops: false,
cyclic_terms: HashMap::new(),
@@ -577,7 +584,7 @@ impl<'a, Outputter: HCValueOutputter> HCPrinter<'a, Outputter>
self.last_item_idx = self.outputter.len();
self.outputter.append(s);
}
fn offset_as_string(&self, iter: &mut HCPreOrderIterator, addr: Addr) -> Option<String>
{
if let Some(var) = self.var_names.get(&addr) {
@@ -781,8 +788,12 @@ impl<'a, Outputter: HCValueOutputter> HCPrinter<'a, Outputter>
self.append_str(&format!("{}", c)),
Constant::EmptyList =>
self.append_str("[]"),
Constant::Number(n) =>
self.print_number(n, op),
Constant::Integer(n) =>
self.print_number(Number::Integer(n), op),
Constant::Float(n) =>
self.print_number(Number::Float(n), op),
Constant::Rational(n) =>
self.print_number(Number::Rational(n), op),
Constant::String(s) =>
self.print_string(s),
Constant::Usize(i) =>
@@ -837,7 +848,9 @@ impl<'a, Outputter: HCValueOutputter> HCPrinter<'a, Outputter>
if self.numbervars && arity == 1 && name.as_str() == "$VAR" {
!iter.immediate_leaf_has_property(|c| {
match c {
Constant::Number(n) => n.is_zero() || n.is_positive(),
Constant::Integer(n) => n >= 0,
Constant::Float(f) => f >= OrderedFloat(0f64),
Constant::Rational(r) => r >= 0,
_ => false
}
}) && needs_bracketing(&spec, op)

View File

@@ -73,7 +73,8 @@ pub enum ArithmeticInstruction {
IntPow(ArithmeticTerm, ArithmeticTerm, usize),
IDiv(ArithmeticTerm, ArithmeticTerm, usize),
Max(ArithmeticTerm, ArithmeticTerm, usize),
FIDiv(ArithmeticTerm, ArithmeticTerm, usize),
Min(ArithmeticTerm, ArithmeticTerm, usize),
IntFloorDiv(ArithmeticTerm, ArithmeticTerm, usize),
RDiv(ArithmeticTerm, ArithmeticTerm, usize),
Div(ArithmeticTerm, ArithmeticTerm, usize),
Shl(ArithmeticTerm, ArithmeticTerm, usize),
@@ -83,8 +84,25 @@ pub enum ArithmeticInstruction {
Or(ArithmeticTerm, ArithmeticTerm, usize),
Mod(ArithmeticTerm, ArithmeticTerm, usize),
Rem(ArithmeticTerm, ArithmeticTerm, usize),
Cos(ArithmeticTerm, usize),
Sin(ArithmeticTerm, usize),
Tan(ArithmeticTerm, usize),
Log(ArithmeticTerm, usize),
Exp(ArithmeticTerm, usize),
ACos(ArithmeticTerm, usize),
ASin(ArithmeticTerm, usize),
ATan(ArithmeticTerm, usize),
ATan2(ArithmeticTerm, ArithmeticTerm, usize),
Sqrt(ArithmeticTerm, usize),
Abs(ArithmeticTerm, usize),
Float(ArithmeticTerm, usize),
Truncate(ArithmeticTerm, usize),
Round(ArithmeticTerm, usize),
Ceiling(ArithmeticTerm, usize),
Floor(ArithmeticTerm, usize),
Neg(ArithmeticTerm, usize),
Plus(ArithmeticTerm, usize),
BitwiseComplement(ArithmeticTerm, usize)
}
pub enum ControlInstruction {

View File

@@ -4,7 +4,6 @@ use prolog::heap_print::*;
use prolog::machine::*;
use prolog::machine::compile::*;
use prolog::machine::machine_errors::*;
use prolog::num::ToPrimitive;
use std::io::Read;
@@ -47,7 +46,7 @@ impl Machine {
};
let arity = match self.machine_st.store(self.machine_st.deref(arity)) {
Addr::Con(Constant::Number(Number::Integer(arity))) =>
Addr::Con(Constant::Integer(arity)) =>
arity.to_usize().unwrap(),
_ => unreachable!()
};
@@ -192,7 +191,7 @@ impl Machine {
{
let index = self.machine_st[temp_v!(3)].clone();
let index = match self.machine_st.store(self.machine_st.deref(index)) {
Addr::Con(Constant::Number(Number::Integer(n))) => n.to_usize().unwrap(),
Addr::Con(Constant::Integer(n)) => n.to_usize().unwrap(),
_ => unreachable!()
};
@@ -227,7 +226,7 @@ impl Machine {
{
let index = self.machine_st[temp_v!(3)].clone();
let index = match self.machine_st.store(self.machine_st.deref(index)) {
Addr::Con(Constant::Number(Number::Integer(n))) => n.to_usize().unwrap(),
Addr::Con(Constant::Integer(n)) => n.to_usize().unwrap(),
_ => unreachable!()
};

View File

@@ -3,9 +3,7 @@ use prolog_parser::string_list::*;
use prolog::machine::machine_indices::*;
use prolog::machine::machine_state::*;
use prolog::num::bigint::BigInt;
use std::rc::Rc;
use prolog::rug::Integer;
pub(super) type MachineStub = Vec<HeapCellValue>;
@@ -23,7 +21,7 @@ pub(super) struct MachineError {
impl MachineError {
pub(super) fn functor_stub(name: ClauseName, arity: usize) -> MachineStub {
let name = HeapCellValue::Addr(Addr::Con(Constant::Atom(name, None)));
functor!("/", 2, [name, heap_integer!(arity)], SharedOpDesc::new(400, YFX))
functor!("/", 2, [name, heap_integer!(Integer::from(arity))], SharedOpDesc::new(400, YFX))
}
pub(super) fn evaluation_error(eval_error: EvalError) -> Self {
@@ -47,7 +45,7 @@ impl MachineError {
let mut stub = functor!("evaluation_error", 1, [HeapCellValue::Addr(Addr::HeapCell(h + 2))]);
stub.append(&mut functor!("/", 2, [HeapCellValue::Addr(Addr::HeapCell(h + 2 + 3)),
heap_integer!(arity)],
heap_integer!(Integer::from(arity))],
SharedOpDesc::new(400, YFX)));
stub.append(&mut functor!(":", 2, [mod_name, name], SharedOpDesc::new(600, XFY)));
@@ -99,7 +97,28 @@ impl MachineError {
MachineError { stub, from: ErrorProvenance::Constructed }
}
fn arithmetic_error(h: usize, err: ArithmeticError) -> Self {
match err {
ArithmeticError::UninstantiatedVar =>
Self::instantiation_error(),
ArithmeticError::NonEvaluableFunctor(name, arity) => {
let name = HeapCellValue::Addr(Addr::Con(name));
let culprit = functor!("/", 2, [name, heap_integer!(Integer::from(arity))],
SharedOpDesc::new(400, YFX));
let mut stub = Self::type_error(ValidType::Evaluable, Addr::HeapCell(3+h)).stub;
stub.extend(culprit.into_iter());
MachineError { stub, from: ErrorProvenance::Constructed }
}
}
}
pub(super) fn syntax_error(h: usize, err: ParserError) -> Self {
if let ParserError::Arithmetic(err) = err {
return Self::arithmetic_error(h, err);
}
let err = vec![heap_atom!(err.as_str())];
let mut stub = if err.len() == 1 {
@@ -175,7 +194,8 @@ pub enum ValidType {
Callable,
Character,
Compound,
// Evaluable,
Evaluable,
Float,
// InByte,
// InCharacter,
Integer,
@@ -196,7 +216,8 @@ impl ValidType {
ValidType::Callable => "callable",
ValidType::Character => "character",
ValidType::Compound => "compound",
// ValidType::Evaluable => "evaluable",
ValidType::Evaluable => "evaluable",
ValidType::Float => "float",
// ValidType::InByte => "in_byte",
// ValidType::InCharacter => "in_character",
ValidType::Integer => "integer",
@@ -249,21 +270,19 @@ impl RepFlag {
// from 7.12.2 g) of 13211-1:1995
#[derive(Clone, Copy)]
pub enum EvalError {
// FloatOverflow,
// Undefined,
// FloatUnderflow,
FloatOverflow,
Undefined,
// Underflow,
ZeroDivisor,
NoRoots
}
impl EvalError {
pub fn as_str(self) -> &'static str {
match self {
// EvalError::FloatOverflow => "float_overflow",
// EvalError::Undefined => "undefined",
EvalError::FloatOverflow => "float_overflow",
EvalError::Undefined => "undefined",
// EvalError::FloatUnderflow => "underflow",
EvalError::ZeroDivisor => "zero_divisor",
EvalError::NoRoots => "no_roots"
}
}
}

View File

@@ -11,8 +11,8 @@ use prolog::machine::machine_errors::*;
use prolog::machine::machine_indices::*;
use prolog::machine::modules::*;
use prolog::machine::or_stack::*;
use prolog::num::{BigInt, BigUint, One, ToPrimitive, Zero};
use prolog::read::PrologStream;
use prolog::rug::Integer;
use downcast::Any;
@@ -20,7 +20,6 @@ use std::cmp::Ordering;
use std::io::{Write, stdout};
use std::mem;
use std::ops::{Index, IndexMut};
use std::rc::Rc;
pub(super) struct Ball {
pub(super) boundary: usize, // ball.0
@@ -288,7 +287,7 @@ impl MachineState {
},
&Addr::Con(Constant::CharCode(c)) =>
codes.push(c),
&Addr::Con(Constant::Number(Number::Integer(ref n))) =>
&Addr::Con(Constant::Integer(ref n)) =>
if let Some(c) = n.to_u8() {
codes.push(c);
} else {
@@ -741,7 +740,7 @@ pub(crate) trait CallPolicy: Any {
let a1 = machine_st[r].clone();
let a2 = machine_st.get_number(at)?;
machine_st.unify(a1, Addr::Con(Constant::Number(a2)));
machine_st.unify(a1, Addr::Con(a2.to_constant()));
return_from_clause!(machine_st.last_call, machine_st)
},
}
@@ -875,8 +874,8 @@ impl CallPolicy for DefaultCallPolicy {}
pub(crate) struct CWILCallPolicy {
pub(crate) prev_policy: Box<CallPolicy>,
count: BigUint,
limits: Vec<(BigUint, usize)>,
count: Integer,
limits: Vec<(Integer, usize)>,
inference_limit_exceeded: bool
}
@@ -887,7 +886,7 @@ impl CWILCallPolicy {
mem::swap(&mut prev_policy, policy);
let new_policy = CWILCallPolicy { prev_policy,
count: BigUint::zero(),
count: Integer::from(0),
limits: vec![],
inference_limit_exceeded: false };
*policy = Box::new(new_policy);
@@ -904,35 +903,32 @@ impl CWILCallPolicy {
return Err(functor!("inference_limit_exceeded", 1,
[HeapCellValue::Addr(Addr::Con(Constant::Usize(bp)))]));
} else {
self.count += BigUint::one();
self.count += 1;
}
}
Ok(())
}
pub(crate) fn add_limit(&mut self, limit: Rc<BigInt>, b: usize) -> Rc<BigInt> {
let limit = match limit.to_biguint() {
Some(limit) => limit + &self.count,
None => panic!("install_inference_counter: limit must be positive")
};
pub(crate) fn add_limit(&mut self, mut limit: Integer, b: usize) -> &Integer {
limit += &self.count;
match self.limits.last().cloned() {
Some((ref inner_limit, _)) if *inner_limit <= limit => {},
_ => self.limits.push((limit, b))
};
Rc::new(BigInt::from(self.count.clone()))
&self.count
}
pub(crate) fn remove_limit(&mut self, b: usize) -> Rc<BigInt> {
pub(crate) fn remove_limit(&mut self, b: usize) -> &Integer {
if let Some((_, bp)) = self.limits.last().cloned() {
if bp == b {
self.limits.pop();
}
}
Rc::new(BigInt::from(self.count.clone()))
&self.count
}
pub(crate) fn is_empty(&self) -> bool {

File diff suppressed because it is too large Load Diff

View File

@@ -11,10 +11,9 @@ use prolog::machine::machine_errors::*;
use prolog::machine::machine_indices::*;
use prolog::machine::machine_state::*;
use prolog::machine::toplevel::{to_op_decl};
use prolog::num::{FromPrimitive, ToPrimitive, Zero};
use prolog::num::bigint::{BigInt};
use prolog::ordered_float::OrderedFloat;
use prolog::read::{PrologStream, readline};
use prolog::rug::Integer;
use ref_thread_local::RefThreadLocal;
@@ -121,7 +120,7 @@ impl MachineState {
fn finalize_skip_max_list(&mut self, n: usize, addr: Addr) {
let target_n = self[temp_v!(1)].clone();
self.unify(Addr::Con(integer!(n)), target_n);
self.unify(Addr::Con(Constant::Integer(Integer::from(n))), target_n);
if !self.fail {
let xs = self[temp_v!(4)].clone();
@@ -133,12 +132,12 @@ impl MachineState {
let max_steps = self.store(self.deref(self[temp_v!(2)].clone()));
match max_steps {
Addr::Con(Constant::Number(Number::Integer(ref max_steps))) =>
Addr::Con(Constant::Integer(ref max_steps)) =>
if max_steps.to_isize().map(|i| i >= -1).unwrap_or(false) {
let n = self.store(self.deref(self[temp_v!(1)].clone()));
match n {
Addr::Con(Constant::Number(Number::Integer(ref n))) if n.is_zero() => {
Addr::Con(Constant::Integer(ref n)) if n == &0 => {
let xs0 = self[temp_v!(3)].clone();
let xs = self[temp_v!(4)].clone();
@@ -163,7 +162,7 @@ impl MachineState {
self.finalize_skip_max_list(n + s.len(),
Addr::Con(Constant::EmptyList))
} else {
let i = max_steps.to_usize().unwrap() - n;
let i = (max_steps as usize) - n;
if s.len() < i {
self.finalize_skip_max_list(n + s.len(),
@@ -338,7 +337,7 @@ impl MachineState {
}
}
fn int_to_char_code(&mut self, n: Rc<BigInt>, stub: &'static str, arity: usize)
fn int_to_char_code(&mut self, n: &Integer, stub: &'static str, arity: usize)
-> Result<u8, MachineStub>
{
if let Some(c) = n.to_u8() {
@@ -381,8 +380,12 @@ impl MachineState {
return Err(self.error_form(err, stub));
},
Ok(Term::Constant(_, Constant::Number(n))) =>
self.unify(nx, Addr::Con(Constant::Number(n))),
Ok(Term::Constant(_, Constant::Rational(n))) =>
self.unify(nx, Addr::Con(Constant::Rational(n))),
Ok(Term::Constant(_, Constant::Float(n))) =>
self.unify(nx, Addr::Con(Constant::Float(n))),
Ok(Term::Constant(_, Constant::Integer(n))) =>
self.unify(nx, Addr::Con(Constant::Integer(n))),
Ok(Term::Constant(_, Constant::CharCode(c))) =>
self.unify(nx, Addr::Con(Constant::CharCode(c))),
_ => {
@@ -519,8 +522,8 @@ impl MachineState {
for addr in addrs.iter() {
match addr {
&Addr::Con(Constant::Number(Number::Integer(ref n))) => {
let c = self.int_to_char_code(n.clone(), "atom_codes", 2)?;
&Addr::Con(Constant::Integer(ref n)) => {
let c = self.int_to_char_code(&n, "atom_codes", 2)?;
chars.push(c as char);
},
&Addr::Con(Constant::CharCode(c)) =>
@@ -550,10 +553,10 @@ impl MachineState {
_ => unreachable!()
};
let len = Number::Integer(Rc::new(BigInt::from_usize(atom.as_str().len()).unwrap()));
let len = Integer::from(atom.as_str().len());
let a2 = self[temp_v!(2)].clone();
self.unify(a2, Addr::Con(Constant::Number(len)));
self.unify(a2, Addr::Con(Constant::Integer(len)));
},
&SystemClauseType::CharsToNumber => {
let stub = MachineError::functor_stub(clause_name!("number_chars"), 2);
@@ -574,9 +577,9 @@ impl MachineState {
let chs = self[temp_v!(2)].clone();
let string = match self.store(self.deref(n)) {
Addr::Con(Constant::Number(Number::Float(OrderedFloat(n)))) =>
Addr::Con(Constant::Float(OrderedFloat(n))) =>
format!("{0:<20?}", n),
Addr::Con(Constant::Number(Number::Integer(n))) =>
Addr::Con(Constant::Integer(n)) =>
n.to_string(),
_ => unreachable!()
};
@@ -591,9 +594,9 @@ impl MachineState {
let chs = self[temp_v!(2)].clone();
let string = match self.store(self.deref(n)) {
Addr::Con(Constant::Number(Number::Float(OrderedFloat(n)))) =>
Addr::Con(Constant::Float(OrderedFloat(n))) =>
format!("{0:<20?}", n),
Addr::Con(Constant::Number(Number::Integer(n))) =>
Addr::Con(Constant::Integer(n)) =>
n.to_string(),
_ => unreachable!()
};
@@ -659,8 +662,8 @@ impl MachineState {
match self.store(self.deref(a2)) {
Addr::Con(Constant::CharCode(code)) =>
self.unify(Addr::Con(Constant::Char(code as char)), addr.clone()),
Addr::Con(Constant::Number(Number::Integer(n))) => {
let c = self.int_to_char_code(n, "char_code", 2)?;
Addr::Con(Constant::Integer(n)) => {
let c = self.int_to_char_code(&n, "char_code", 2)?;
self.unify(Addr::Con(Constant::Char(c as char)), addr.clone());
},
_ => self.fail = true
@@ -967,12 +970,12 @@ impl MachineState {
let a2 = self[temp_v!(2)].clone();
let a3 = self[temp_v!(3)].clone();
let arity = Number::Integer(Rc::new(BigInt::from_usize(arity).unwrap()));
let arity = Integer::from(arity);
self.unify(a2, Addr::Con(Constant::Atom(name, spec)));
if !self.fail {
self.unify(a3, Addr::Con(Constant::Number(arity)));
self.unify(a3, Addr::Con(Constant::Integer(arity)));
}
},
_ => self.fail = true
@@ -1007,11 +1010,11 @@ impl MachineState {
}
};
let a2 = Number::Integer(Rc::new(BigInt::from_usize(priority).unwrap()));
let a2 = Integer::from(priority);
let a3 = Addr::Con(Constant::Atom(clause_name!(spec), None));
let a4 = Addr::Con(Constant::Atom(name, Some(shared_op_desc)));
self.unify(Addr::Con(Constant::Number(a2)), prec);
self.unify(Addr::Con(Constant::Integer(a2)), prec);
if !self.fail {
self.unify(a3, specifier);
@@ -1032,7 +1035,7 @@ impl MachineState {
let op = self[temp_v!(3)].clone();
let priority = match self.store(self.deref(priority)) {
Addr::Con(Constant::Number(Number::Integer(n))) => n.to_usize().unwrap(),
Addr::Con(Constant::Integer(n)) => n.to_usize().unwrap(),
_ => unreachable!()
};
@@ -1251,11 +1254,11 @@ impl MachineState {
match (a1, a2.clone()) {
(Addr::Con(Constant::Usize(bp)),
Addr::Con(Constant::Number(Number::Integer(n)))) =>
Addr::Con(Constant::Integer(n))) =>
match call_policy.downcast_mut::<CWILCallPolicy>().ok() {
Some(call_policy) => {
let count = call_policy.add_limit(n, bp);
let count = Addr::Con(Constant::Number(Number::Integer(count)));
let count = Addr::Con(Constant::Integer(count.clone()));
let a3 = self[temp_v!(3)].clone();
@@ -1369,7 +1372,7 @@ impl MachineState {
if let Addr::Con(Constant::Usize(bp)) = a1 {
let count = call_policy.remove_limit(bp);
let count = Addr::Con(Constant::Number(Number::Integer(count)));
let count = Addr::Con(Constant::Integer(count.clone()));
let a2 = self[temp_v!(2)].clone();
@@ -1692,7 +1695,7 @@ impl MachineState {
if var_names.contains_key(&var) {
continue;
}
var_names.insert(var, atom);
},
_ => unreachable!()

View File

@@ -3,7 +3,8 @@ use prolog_parser::parser::*;
use prolog::machine::*;
use prolog::machine::machine_indices::HeapCellValue;
use prolog::num::*;
use prolog::rug::Integer;
use prolog::rug::ops::Pow;
use std::cell::Cell;
use std::collections::VecDeque;
@@ -306,7 +307,7 @@ impl MachineState {
// names in the pre-expansion term. This formula ensures that all generated "numbervars"-
// style variable names will be longer than the keys of the var_dict, and therefore
// not equal to any of them.
printer.numbervars_offset = pow(BigInt::from(10), max_var_length) * 26;
printer.numbervars_offset = Integer::from(10).pow(max_var_length as u32) * 26;
printer.drop_toplevel_spec();
printer.see_all_locs();

View File

@@ -9,7 +9,6 @@ use prolog::machine::machine_errors::*;
use prolog::machine::machine_indices::*;
use prolog::machine::machine_state::MachineState;
use prolog::machine::term_expansion::*;
use prolog::num::*;
use std::borrow::BorrowMut;
use std::collections::{HashMap, HashSet, VecDeque};
@@ -142,7 +141,7 @@ fn setup_op_decl(mut terms: Vec<Box<Term>>) -> Result<OpDecl, ParserError>
};
let prec = match *terms.pop().unwrap() {
Term::Constant(_, Constant::Number(Number::Integer(bi))) =>
Term::Constant(_, Constant::Integer(bi)) =>
match bi.to_usize() {
Some(n) if n <= 1200 => n,
_ => return Err(ParserError::InconsistentEntry)
@@ -162,7 +161,7 @@ fn setup_predicate_indicator(mut term: Term) -> Result<PredicateKey, ParserError
let name = *terms.pop().unwrap();
let arity = arity.to_constant().and_then(|c| c.to_integer())
.and_then(|n| if !n.is_negative() { n.to_usize() } else { None })
.and_then(|n| n.to_usize())
.ok_or(ParserError::InvalidModuleExport)?;
let name = name.to_constant().and_then(|c| c.to_atom())

View File

@@ -12,7 +12,7 @@ macro_rules! heap_str {
macro_rules! heap_integer {
($i:expr) => (
HeapCellValue::Addr(Addr::Con(integer!($i)))
HeapCellValue::Addr(Addr::Con(Constant::Integer($i)))
)
}

View File

@@ -1,6 +1,6 @@
extern crate num;
extern crate ordered_float;
extern crate prolog_parser;
extern crate rug;
pub mod instructions;
#[macro_use] mod macros;

View File

@@ -276,6 +276,16 @@ impl fmt::Display for SessionError {
}
}
impl fmt::Display for Number {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self {
&Number::Float(fl) => write!(f, "{}", fl),
&Number::Integer(ref bi) => write!(f, "{}", bi),
&Number::Rational(ref r) => write!(f, "{}", r)
}
}
}
impl fmt::Display for ArithmeticTerm {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self {
@@ -307,8 +317,10 @@ impl fmt::Display for ArithmeticInstruction {
write!(f, "idiv {}, {}, @{}", a1, a2, t),
&ArithmeticInstruction::Max(ref a1, ref a2, ref t) =>
write!(f, "max {}, {}, @{}", a1, a2, t),
&ArithmeticInstruction::FIDiv(ref a1, ref a2, ref t) =>
write!(f, "floored_idiv {}, {}, @{}", a1, a2, t),
&ArithmeticInstruction::Min(ref a1, ref a2, ref t) =>
write!(f, "min {}, {}, @{}", a1, a2, t),
&ArithmeticInstruction::IntFloorDiv(ref a1, ref a2, ref t) =>
write!(f, "int_floor_div {}, {}, @{}", a1, a2, t),
&ArithmeticInstruction::RDiv(ref a1, ref a2, ref t) =>
write!(f, "rdiv {}, {}, @{}", a1, a2, t),
&ArithmeticInstruction::Shl(ref a1, ref a2, ref t) =>
@@ -325,8 +337,42 @@ impl fmt::Display for ArithmeticInstruction {
write!(f, "mod {}, {}, @{}", a1, a2, t),
&ArithmeticInstruction::Rem(ref a1, ref a2, ref t) =>
write!(f, "rem {}, {}, @{}", a1, a2, t),
&ArithmeticInstruction::ATan2(ref a1, ref a2, ref t) =>
write!(f, "atan2 {}, {}, @{}", a1, a2, t),
&ArithmeticInstruction::Plus(ref a, ref t) =>
write!(f, "plus {}, @{}", a, t),
&ArithmeticInstruction::Neg(ref a, ref t) =>
write!(f, "neg {}, @{}", a, t)
write!(f, "neg {}, @{}", a, t),
&ArithmeticInstruction::Cos(ref a, ref t) =>
write!(f, "cos {}, @{}", a, t),
&ArithmeticInstruction::Sin(ref a, ref t) =>
write!(f, "sin {}, @{}", a, t),
&ArithmeticInstruction::Tan(ref a, ref t) =>
write!(f, "tan {}, @{}", a, t),
&ArithmeticInstruction::ATan(ref a, ref t) =>
write!(f, "atan {}, @{}", a, t),
&ArithmeticInstruction::ASin(ref a, ref t) =>
write!(f, "asin {}, @{}", a, t),
&ArithmeticInstruction::ACos(ref a, ref t) =>
write!(f, "acos {}, @{}", a, t),
&ArithmeticInstruction::Log(ref a, ref t) =>
write!(f, "log {}, @{}", a, t),
&ArithmeticInstruction::Exp(ref a, ref t) =>
write!(f, "exp {}, @{}", a, t),
&ArithmeticInstruction::Sqrt(ref a, ref t) =>
write!(f, "sqrt {}, @{}", a, t),
&ArithmeticInstruction::BitwiseComplement(ref a, ref t) =>
write!(f, "bitwise_complement {}, @{}", a, t),
&ArithmeticInstruction::Truncate(ref a, ref t) =>
write!(f, "truncate {}, @{}", a, t),
&ArithmeticInstruction::Round(ref a, ref t) =>
write!(f, "round {}, @{}", a, t),
&ArithmeticInstruction::Ceiling(ref a, ref t) =>
write!(f, "ceiling {}, @{}", a, t),
&ArithmeticInstruction::Floor(ref a, ref t) =>
write!(f, "floor {}, @{}", a, t),
&ArithmeticInstruction::Float(ref a, ref t) =>
write!(f, "float {}, @{}", a, t),
}
}
}

View File

@@ -1016,8 +1016,8 @@ fn test_queries_on_arithmetic()
assert_prolog_success!(&mut wam, "X is ((3 + 4) // 2) + 2 - 1 // 1, Y is 2+2, Z = 8, Y is 4.",
[["Y = 4", "X = 4", "Z = 8"]]);
assert_prolog_success!(&mut wam, "X is (3 rdiv 4) / 2, Y is 3 rdiv 8, X = Y.",
[["X = 3/8", "Y = 3/8"]]);
assert_prolog_success!(&mut wam, "X is (3 rdiv 4) / 2, Y is 3 rdiv 8.",
[["X = 0.375", "Y = 3/8"]]);
assert_prolog_success!(&mut wam, "X is 10 xor -4, X is -10.", [["X = -10"]]);
assert_prolog_success!(&mut wam, "X is 4 xor -7, X is -3.", [["X = -3"]]);
@@ -1066,8 +1066,6 @@ fn test_queries_on_arithmetic()
[["X = 1"]]);
assert_prolog_success!(&mut wam, "X is 3 ** 1.",
[["X = 3"]]);
assert_prolog_success!(&mut wam, "X is 3 ** -3.",
[["X = 1/27"]]);
assert_prolog_success!(&mut wam, "X is (-3) ** 3.",
[["X = -27"]]);
assert_prolog_success!(&mut wam, "X is (-3) ** 3.",
@@ -1078,38 +1076,35 @@ fn test_queries_on_arithmetic()
[["X = 1"]]);
assert_prolog_success!(&mut wam, "X is (-3) ** 1.",
[["X = -3"]]);
assert_prolog_success!(&mut wam, "X is (-3) ** -3.",
[["X = -1/27"]]);
assert_prolog_success!(&mut wam, "X is (1 rdiv 27) ** -3, X ~ 19683.");
assert_prolog_success!(&mut wam, "X is (-1 rdiv 27) ** -3, X ~ -19683.");
// assert_prolog_success!(&mut wam, "X is (1 rdiv 27) ** -3, X ~ 19683.");
// assert_prolog_success!(&mut wam, "X is (-1 rdiv 27) ** -3, X ~ -19683.");
assert_prolog_success!(&mut wam, "X is 0.0 ** 0.",
[["X = 1.0"]]);
assert_prolog_success!(&mut wam, "catch(_ is 0.0 ** -2342, error(E, _), true).",
[["E = evaluation_error(no_roots)"]]);
[["E = evaluation_error(undefined)"]]);
assert_prolog_success!(&mut wam, "X is 0.0 ** 2342.",
[["X = 0"]]);
assert_prolog_success!(&mut wam, "catch(_ is (-3) ** (1 rdiv 2), error(E, _), true).",
[["E = evaluation_error(no_roots)"]]);
[["E = evaluation_error(undefined)"]]);
assert_prolog_success!(&mut wam, "catch(_ is (-3/2) ** (1 rdiv 2), error(E, _), true).",
[["E = evaluation_error(no_roots)"]]);
[["E = evaluation_error(undefined)"]]);
assert_prolog_success!(&mut wam, "catch(_ is (-3 rdiv 2) ** (1 rdiv 4), error(E, _), true).",
[["E = evaluation_error(no_roots)"]]);
[["E = evaluation_error(undefined)"]]);
assert_prolog_success!(&mut wam, "catch(_ is (-3 rdiv 2) ** (-1 rdiv 4), error(E, _), true).",
[["E = evaluation_error(no_roots)"]]);
[["E = evaluation_error(undefined)"]]);
assert_prolog_success!(&mut wam, "catch(_ is 0 ** (-5 rdiv 4), error(E, _), true).",
[["E = evaluation_error(no_roots)"]]);
[["E = evaluation_error(undefined)"]]);
assert_prolog_success!(&mut wam, "X is 3 ** (1 rdiv 3), Y is X ** 3, Y ~ 3.");
assert_prolog_success!(&mut wam, "X is (-3) ** (1 rdiv 3), Y is X ** 3, Y ~ -3.");
assert_prolog_failure!(&mut wam, "X is (-5) ** (1 rdiv 3), Y is X ** 3, Y ~ -3.");
assert_prolog_failure!(&mut wam, "X is 5 ** (1 rdiv 3), Y is X ** 3, Y ~ 3.");
assert_prolog_failure!(&mut wam, "X is (1 rdiv 3) ** 0.5, Y is X ** 2, X ~ Y.");
// assert_prolog_success!(&mut wam, "X is (-3) ** (1 rdiv 3), Y is X ** 3, Y ~ -3.");
// assert_prolog_failure!(&mut wam, "X is (-5) ** (1 rdiv 3), Y is X ** 3, Y ~ -3.");
assert_prolog_success!(&mut wam, "X is 5 ** (1 rdiv 3), Y is X ** 3, Y ~ 5.");
assert_prolog_success!(&mut wam, "X is (1 rdiv 3) ** 0.5, Y is X ** 2, 1 rdiv 3 ~ Y.");
assert_prolog_success!(&mut wam, "X is (-5) ** (-1 rdiv 3), Y is X ** 3, Y ~ -1 rdiv 5.");
assert_prolog_failure!(&mut wam, "X is (-5) ** (-1 rdiv 3), Y is X ** 3, Y ~ 1 rdiv 5.");
// assert_prolog_success!(&mut wam, "X is (-5) ** (-1 rdiv 3), Y is X ** 3, Y ~ -1 rdiv 5.");
// assert_prolog_failure!(&mut wam, "X is (-5) ** (-1 rdiv 3), Y is X ** 3, Y ~ 1 rdiv 5.");
assert_prolog_success!(&mut wam, "X is (0 rdiv 5) ** 5.",
[["X = 0"]]);
@@ -1612,9 +1607,9 @@ fn test_queries_on_builtins()
assert_prolog_failure!(&mut wam, "[] @< \"string\".");
assert_prolog_failure!(&mut wam, "[] @< atom.");
assert_prolog_success!(&mut wam, "atom @< [].");
assert_prolog_failure!(&mut wam, "1.1 @< 1.");
assert_prolog_success!(&mut wam, "1.1 @< 1.");
assert_prolog_success!(&mut wam, "1.0 @=< 1.");
assert_prolog_success!(&mut wam, "1 @=< 1.0."); //TODO: currently this succeeds. make it fail.
assert_prolog_success!(&mut wam, "1 @=< 1.0.");
submit(&mut wam, ":- use_module(library(non_iso)).");