:- set_prolog_flag(flowlog_engine, wam).

len([], 0).
len([_|T], N) :- len(T, N0), N is N0 + 1.

ok :-
    % char_code/2
    char_code('a', 97),
    char_code('a', C1),
    C1 == 97,
    char_code(Ch, 97),
    Ch == 'a',
    \+ char_code('a', 98),
    catch(char_code(_,_), Ecc0, true),
    Ecc0 == error(instantiation_error, char_code/2),
    catch(char_code(97,_), Ecc1, true),
    Ecc1 == error(type_error(character, 97), char_code/2),
    catch(char_code(_, 256), Ecc2, true),
    Ecc2 == error(representation_error(character_code), char_code/2),

    % atom_length/2
    atom_length(hello, 5),
    atom_length(hello, L0),
    L0 == 5,
    \+ atom_length(hello, 6),
    catch(atom_length(_, _), Eal0, true),
    Eal0 == error(instantiation_error, atom),

    % atom_concat/3 (both directions)
    atom_concat(he, llo, hello),
    atom_concat(he, llo, C2),
    C2 == hello,
    findall(A-B, atom_concat(A, B, ab), Splits),
    Splits == [''-ab, a-b, ab-''],
    catch(atom_concat(_, _, _), Eac0, true),
    Eac0 == error(instantiation_error, atom_concat/3),

    % sub_atom/5
    sub_atom(abc, 0, 1, 2, a),
    sub_atom(abc, 1, 1, 1, b),
    sub_atom(abc, 2, 1, 0, c),
    findall(S, sub_atom(ab, _, _, _, S), Subs),
    len(Subs, 6),

    % atom_chars/2 and atom_codes/2
    atom_chars(abc, [a,b,c]),
    atom_chars(abc, Chs0),
    Chs0 == [a,b,c],
    atom_chars(A3, [a,b]),
    A3 == ab,
    catch(atom_chars(_, [_|_]), Echars0, true),
    Echars0 == error(instantiation_error, atom_chars/2),
    catch(atom_chars(abc, [97]), Echars1, true),
    Echars1 == error(type_error(character, 97), atom_chars/2),

    atom_codes(abc, [97,98,99]),
    atom_codes(abc, Codes0),
    Codes0 == [97,98,99],
    atom_codes(A4, [97,98]),
    A4 == ab,
    catch(atom_codes(abc, [a,98,99]), Ecodes0, true),
    Ecodes0 == error(type_error(integer, a), atom_codes/2),
    catch(atom_codes(_, [-1]), Ecodes1, true),
    Ecodes1 == error(representation_error(character_code), atom_codes/2),

    % number_chars/2 and number_codes/2
    number_chars(123, ['1','2','3']),
    number_chars(123, NChs0),
    NChs0 == ['1','2','3'],
    number_chars(N1, ['4','2']),
    N1 == 42,
    catch(number_chars(_, _), Enc0, true),
    Enc0 == error(instantiation_error, number_chars/2),
    catch(number_chars(a, _), Enc1, true),
    Enc1 == error(type_error(number, a), number_chars/2),
    catch(number_chars(_, [x]), Enc2, true),
    Enc2 == error(syntax_error(illegal_number), number_chars/2),

    number_codes(42, [52,50]),
    number_codes(42, NCodes0),
    NCodes0 == [52,50],
    number_codes(N2, [45,49]),
    N2 == -1.
