% simple compound checker with limited dictionary
% usage: start Prolog, load the file and run isCompound(abc).
%        where abc is some string
% 960312 erik.tjong@ling.uu.se
% 970301 modified main predicate to make it more efficient

% dictionary definition:
% word definitions: word,class
word(konflikt,n).
word(forskning,n).
word(vetenskap,n).
word(miljö,n).

% well formedness table (Vosse p65)
% a compound of a noun and a noun is allowed and yields another noun
allowed(n,n,n).   

% compound/2: non-recursive compound checker
% argument:   Word: input word
isCompound(Word):-
   % break up word in two parts (no test for binding morphemes)
   divideWord(Word,Prefix,Suffix),
   % check if the prefix and the suffix are legal words
   word(Prefix,PrefixClass),
   word(Suffix,SuffixClass),
   % the combination of the words should be allowed   
   allowed(PrefixClass,SuffixClass,WordClass),
   % all tests have been passed: we have a legal compound
   writeln([Word,' is a legal class ',WordClass,' compound.']),
   writeln(['It can be divided in ',Prefix,' and ',Suffix,'.']).

% divideWord/3: divide a string in a prefix and a suffix
% arguments:    Word: input string 
%               Prefix: output prefix
%               Suffix: output suffix 
divideWord(Word,Prefix,Suffix):-
   % divide word in characters
   name(Word,WordChars),
   % divide character list in two groups
   append(PrefixChars,SuffixChars,WordChars),
   % the prefix and suffix lists may not be empty
   PrefixChars \== [],
   SuffixChars \== [],
   % create strings for the prefix and suffix
   name(Prefix,PrefixChars),
   name(Suffix,SuffixChars).

% append/3: append two strings
append([X|Xs],Ys,[X|Zs]):-
   append(Xs,Ys,Zs).
append([],Ys,Ys).

% writeln/1: write a list of terms
% argument:  X: list of strings
% source: Leon Sterling and Ehud Shapiro, "The Art of Prolog -
%         Advanced Programming Techniques". The MIT Press, 1986. 
%         ISBN 0-262-69105-1. Page 176 
writeln([X|Xs]):-
   write(X),
   writeln(Xs).
writeln([]):-nl.
