Mercury 勉強メモ

関数論理型言語 Mercury を勉強するブログです.

よく使う述語: エラー・真偽値・文字・整数編

Mercury の標準ライブラリから, よく使いそうな述語をピックアップしました.

require モジュール

error

:- pred error(string::in) is erroneous.

エラーメッセージを表示してプログラムを停止する.

bool モジュール

bool 型

:- type bool
    --->    no
    ;       yes.

bool 演算

:- func bool.not(bool) = bool.
:- func bool.and(bool, bool) = bool.
:- func bool.or(bool, bool) = bool.
:- func bool.xor(bool, bool) = bool.

名前の通り.

char モジュール

char.to_int

:- func char.to_int(char) = int.
:- pred char.to_int(char, int).
:- mode char.to_int(in, out) is det.
:- mode char.to_int(in, in) is semidet.
:- mode char.to_int(out, in) is semidet.

文字を文字コードに変換

char.from_int

:- pred char.from_int(int::in, char::out) is semidet.

文字コードを文字に変換

char.digit_to_int

:- pred char.digit_to_int(char::in, int::out) is semidet.

数字を整数に変換

char.int_to_digit

:- pred char.int_to_digit(int, char).
:- mode char.int_to_digit(in, out) is semidet.
:- mode char.int_to_digit(out, in) is semidet.

整数を数字に変換

char.to_upper

:- func char.to_upper(char) = char.
:- pred char.to_upper(char::in, char::out) is det.

英字を大文字にする.

char.to_lower

:- func char.to_lower(char) = char.
:- pred char.to_lower(char::in, char::out) is det.

英字を小文字にする.

文字クラスの判定

:- pred char.is_whitespace(char::in) is semidet.
:- pred char.is_upper(char::in) is semidet.
:- pred char.is_lower(char::in) is semidet.
:- pred char.is_alpha(char::in) is semidet.
:- pred char.is_alnum(char::in) is semidet.
:- pred char.is_alpha_or_underscore(char::in) is semidet.
:- pred char.is_alnum_or_underscore(char::in) is semidet.
:- pred char.is_digit(char::in) is semidet.
:- pred char.is_binary_digit(char::in) is semidet.
:- pred char.is_octal_digit(char::in) is semidet.
:- pred char.is_hex_digit(char::in) is semidet.

名前の通り.

int モジュール

(int) +

:- func int + int = int.
:- mode in  + in  = uo  is det.
:- mode uo  + in  = in  is det.
:- mode in  + uo  = in  is det.

加算.逆演算ができる.

(int) -

:- func int - int = int.
:- mode in  - in  = uo  is det.
:- mode uo  - in  = in  is det.
:- mode in  - uo  = in  is det.

減算.逆演算ができる.

(int) *

:- func (int::in) * (int::in) = (int::uo) is det.

乗算.

基本演算

:- func int.plus(int, int) = int.
:- func int.minus(int, int) = int.
:- func int.times(int, int) = int.
:- func div(int::in, int::in) = (int::uo) is det.
:- func mod(int::in, int::in) = (int::uo) is det.

比較演算

:- pred (int::in) < (int::in) is semidet.
:- pred (int::in) > (int::in) is semidet.
:- pred (int::in) =< (int::in) is semidet.
:- pred (int::in) >= (int::in) is semidet.

int.abs

:- func int.abs(int) = int.
:- pred int.abs(int::in, int::out) is det.

絶対値

int.max

:- func int.max(int, int) = int.
:- pred int.max(int::in, int::in, int::out) is det.

値が大きい方を返す.

int.mix

:- func int.min(int, int) = int. :- pred int.min(int::in, int::in, int::out) is det.

値が小さい方を返す.