% ---------------------------------------------------------------------------- %
% spellcheck.m
% Ralph Becket <rbeck@microsoft.com>
% Tue Jan  9 16:43:59 GMT 2001
% vim: ts=4 sw=4 et tw=0 wm=0 ff=unix
%
% ---------------------------------------------------------------------------- %

:- module mytest.

:- interface.

:- import_module io.



:- pred main(io__state, io__state).
:- mode main(di, uo) is det.



:- implementation.

:- import_module map, int, list, string, require, std_util, char.



:- type dict == map(string, unit).



main -->
    io__see("Usr.Dict.Words", Res),
    (   { Res = ok },           read_dictionary(map__init, Dict)
    ;   { Res = error(ErrNo) }, { error(io__error_message(ErrNo)) }
    ),
    io__seen,
    find_errors(Dict).



:- pred read_dictionary(dict, dict, io__state, io__state).
:- mode read_dictionary(in, out, di, uo) is det.

read_dictionary(D0, D) -->
    io__read_line_as_string(Result),
    (
        { Result = ok(Line) },
        { Words  = string__words(char__is_whitespace, Line) },
        { D1     = list__foldl(func(W, M) = M ^ elem(W) := unit, Words, D0) },
        read_dictionary(D1, D)
    ;
        { Result = eof },
        { D      = D0 }
    ;
        { Result = error(ErrNo) },
        { error(io__error_message(ErrNo)) }
    ).



:- pred find_errors(dict, io__state, io__state).
:- mode find_errors(in, di, uo) is det.

find_errors(D) -->
    io__read_line_as_string(Result),
    (
        { Result = ok(Line) },
        { Words  = string__words(char__is_whitespace, Line) },
        list__foldl(
            ( pred(S::in, di, uo) is det -->
                ( if { map__contains(D, S) }
                  then []
                  else io__write_string(S), io__nl
                )
            ),
            Words
        ),
        find_errors(D)
    ;
        { Result = eof }
    ;
        { Result = error(ErrNo) },
        { error(io__error_message(ErrNo)) }
    ).