1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
//
// This file is part of zero_sum.
//
// zero_sum is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// zero_sum is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with zero_sum. If not, see <http://www.gnu.org/licenses/>.
//
// Copyright 2016 Chris Foster
//

use std::fmt::Display;
use std::ops::{Add, Div, Mul, Neg, Sub};

use ply::Ply;
use resolution::Resolution;
use state::State;

/// An evaluation type.
///
/// This is usually a tuple around a signed numeric type.
///
/// # Example
///
/// There is a [helper macro](../macro.prepare_evaluation_tuple.html) to facilitate the implementation of tuple structs:
///
/// ```rust
/// #[macro_use]
/// extern crate zero_sum;
/// # use zero_sum::analysis::Evaluation;
/// # use std::fmt;
/// use std::i32;
/// use std::ops::{Add, Div, Mul, Neg, Sub};
///
/// #[derive(Clone, Copy, PartialEq, PartialOrd)]
/// struct Eval(i32);
///
/// prepare_evaluation_tuple!(Eval); // impl Add, Div, Mul, Neg, Sub, and Display
///
/// impl Evaluation for Eval {
///     fn null() -> Eval { Eval(0) }
///     fn epsilon() -> Eval { Eval(1) }
///     fn win() -> Eval { Eval(100000) }
///     fn max() -> Eval { Eval(i32::MAX) }
///     fn is_win(&self) -> bool { self.0.abs() > 99000 }
/// }
/// # fn main() { }
/// ```
pub trait Evaluation:
    Sized +
    Clone +
    Copy +
    Display +
    Add<Output = Self> +
    Sub<Output = Self> +
    Mul<Output = Self> +
    Neg<Output = Self> +
    Div<Output = Self> +
    PartialEq +
    PartialOrd {
    /// An empty, or zero evaluation.
    fn null() -> Self;
    /// The smallest step to consider.
    fn epsilon() -> Self;
    /// The base value of a win.  The evaluation system may add or subtract to it in
    /// in order to promote it or discourage it in favor of others in the search.
    fn win() -> Self;
    /// The maximum value representable.  This must be safely negatable.
    fn max() -> Self;
    /// Returns `true` if this evaluation contains a win.  This is usually a check to
    /// see if the absolute value is above a certain threshold.
    fn is_win(&self) -> bool;
}

/// Provides evaluation capabilities.
///
/// This is usually implemented on a `State`.
pub trait Evaluatable<E> where
    E: Evaluation {
    /// Returns the evaluation of the current state.
    fn evaluate(&self) -> E;

    /// Returns the evaluation of the state after executing `plies`.
    ///
    /// # Panics
    /// Will panic if the execution of any ply in `plies` causes an error.
    fn evaluate_plies<P, R>(&self, plies: &[P]) -> E where P: Ply, R: Resolution, Self: State<P, R> {
        match self.execute_plies(plies) {
            Ok(state) => if plies.len() % 2 == 0 {
                state.evaluate()
            } else {
                -state.evaluate()
            },
            Err(error) => panic!("Error calculating evaluation: {}", error),
        }
    }
}

/// Implement arithmetic operators (`Add`, `Sub`, `Mul`, `Neg`, `Div`) and `Display` for a tuple
/// struct in terms of the enclosed type.
///
/// # Example
///
/// ```rust
/// #[macro_use]
/// extern crate zero_sum;
/// # use zero_sum::analysis::Evaluation;
/// # use std::fmt;
/// use std::i32;
/// use std::ops::{Add, Div, Mul, Neg, Sub};
///
/// #[derive(Clone, Copy, PartialEq, PartialOrd)]
/// struct Eval(i32);
///
/// prepare_evaluation_tuple!(Eval); // impl Add, Div, Mul, Neg, Sub, and Display
///
/// impl Evaluation for Eval {
///     fn null() -> Eval { Eval(0) }
///     fn epsilon() -> Eval { Eval(1) }
///     fn win() -> Eval { Eval(100000) }
///     fn max() -> Eval { Eval(i32::MAX) }
///     fn is_win(&self) -> bool { self.0.abs() > 99000 }
/// }
/// # fn main() { }
/// ```
#[macro_export]
macro_rules! prepare_evaluation_tuple {
    ($type_: ident) => {
        impl_tuple_operation! { impl Add for $type_ { fn add } }
        impl_tuple_operation! { impl Sub for $type_ { fn sub } }
        impl_tuple_operation! { impl Mul for $type_ { fn mul } }
        impl_tuple_operation! { impl Div for $type_ { fn div } }

        impl Neg for $type_ {
            type Output = $type_;
            fn neg(self) -> $type_ {
                let $type_(a) = self;
                $type_(-a)
            }
        }

        impl std::fmt::Display for $type_ {
            fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
                let $type_(a) = *self;
                write!(f, "{}", a)
            }
        }
    }
}

/// Implement a binary operation for a tuple struct in terms of the enclosed type.
///
/// Usually, it shouldn't be necessary to use this macro directly; instead consider
/// using [prepare_evaluation_tuple](macro.prepare_evaluation_tuple.html).
#[macro_export]
macro_rules! impl_tuple_operation {
    (impl $trait_: ident for $type_: ident { fn $method: ident }) => {
        impl $trait_ for $type_ {
            type Output = $type_;
            fn $method(self, $type_(b): $type_) -> $type_ {
                let $type_(a) = self;
                $type_(a.$method(&b))
            }
        }
    }
}