packages feed

ychr-0.1.0.0: libraries/prelude.chr

:- module(prelude, [
    type(bool/0),
    type(list/1),
    fun '+'/2,
    fun '-'/2,
    fun '*'/2,
    fun '/'/2,
    fun 'div'/2,
    fun 'mod'/2,
    fun 'rem'/2,
    fun '<'/2,
    fun '>'/2,
    fun '>='/2,
    fun '=<'/2,
    fun '=='/2,
    fun not/1,
    fun max/2,
    fun min/2,
    fun var/1,
    fun nonvar/1,
    fun integer/1,
    fun float/1,
    fun atom/1,
    fun boolean/1,
    fun string/1,
    fun ground/1,
    fun int_to_float/1,
    fun float_to_int/1,
    fun unifiable/2,
    fun term_variables/1,
    fun compound_to_list/1,
    fun list_to_compound/1,
    fun copy_term/1,
    fun write/1,
    fun nl/0,
    fun writeln/1,
    fun call/2,
    fun call/3,
    op(500, yfx, '+'),
    op(500, yfx, '-'),
    op(400, yfx, '*'),
    op(400, yfx, '/'),
    op(400, yfx, div),
    op(400, yfx, mod),
    op(400, yfx, rem),
    op(700, xfx, '<'),
    op(700, xfx, '>'),
    op(700, xfx, '>='),
    op(700, xfx, '=<'),
    op(700, xfx, '==')
]).

:- chr_type bool ---> true ; false.

:- chr_type list(T) ---> [] ; [T|list(T)].

:- class
    ('+'(int, int) -> int),
    ('+'(float, float) -> float).
:- class
    ('-'(int, int) -> int),
    ('-'(float, float) -> float).
:- class
    ('*'(int, int) -> int),
    ('*'(float, float) -> float).
:- class
    ('<'(int, int) -> bool),
    ('<'(float, float) -> bool).
:- class
    ('>'(int, int) -> bool),
    ('>'(float, float) -> bool).
:- class
    ('>='(int, int) -> bool),
    ('>='(float, float) -> bool).
:- class
    ('=<'(int, int) -> bool),
    ('=<'(float, float) -> bool).

:- function
    ('/'(float, float) -> float),
    ('div'(int, int) -> int),
    ('mod'(int, int) -> int),
    ('rem'(int, int) -> int),
    ('=='(A, A) -> bool),
    (not(bool) -> bool),
    (var(any) -> bool),
    (nonvar(any) -> bool),
    (integer(any) -> bool),
    (float(any) -> bool),
    (atom(any) -> bool),
    (boolean(any) -> bool),
    (string(any) -> bool),
    (ground(any) -> bool),
    (int_to_float(int) -> float),
    (float_to_int(float) -> int),
    (copy_term(A) -> A),
    (unifiable(any, any) -> bool),
    term_variables/1,
    (compound_to_list(any) -> list(any)),
    (list_to_compound(list(any)) -> any),
    (call(fun(A) -> B end, A) -> B),
    (call(fun(A, B) -> C end, A, B) -> C),
    (write(string) -> any),
    (nl() -> any),
    (writeln(string) -> any).

:- function max(T, T) -> T requiring '>='(T, T) -> bool.
:- function min(T, T) -> T requiring '=<'(T, T) -> bool.

X + Y -> host:'+'(X, Y).

X - Y -> host:'-'(X, Y).

X * Y -> host:'*'(X, Y).

X / Y -> host:'/'(X, Y).

X div Y -> host:'div'(X, Y).

X mod Y -> host:'mod'(X, Y).

X rem Y -> host:'rem'(X, Y).

X < Y -> host:'<'(X, Y).

X > Y -> host:'>'(X, Y).

X >= Y -> host:'>='(X, Y).

X =< Y -> host:'=<'(X, Y).

X == Y -> host:'=='(X, Y).

% Boolean negation. There is no `\=` / `\==` operator; write the
% negation explicitly, e.g. `not(X == Y)` or `not(unifiable(X, Y))`.
not(X) -> host:not(X).

max(X, Y) | X >= Y -> X.
max(_, Y) -> Y.

min(X, Y) | X =< Y -> X.
min(_, Y) -> Y.

var(X) -> host:var(X).

nonvar(X) -> host:nonvar(X).

integer(X) -> host:integer(X).

float(X) -> host:float(X).

int_to_float(X) -> host:int_to_float(X).

float_to_int(X) -> host:float_to_int(X).

atom(X) -> host:atom(X).

boolean(X) -> host:boolean(X).

string(X) -> host:string(X).

ground(X) -> host:ground(X).

unifiable(X, Y) -> host:unifiable(X, Y).

term_variables(T) -> host:term_variables(T).

compound_to_list(C) -> host:compound_to_list(C).

list_to_compound(Xs) -> host:list_to_compound(Xs).

write(S) -> host:write(S).

nl -> host:write("\n").

writeln(S) -> host:writeln(S).

copy_term(T) -> host:copy_term(T).

call(F, X) -> '$call'(F, X).

call(F, X, Y) -> '$call'(F, X, Y).