2:- module(search, [search_by_name/3, search_languages/4]).    3
    4:- use_module(dataset).    5
    6match_language(Language, AreaOfInterest, Paradigm, TypeSystem) :-
    7    language(_, _, About, _, LanguageTypes, LanguageParadigm, _) = Language,
    8    (
    9        (About \= '', AreaOfInterest \= ''
   10        -> downcase_atom(About, AboutLowered),
   11           downcase_atom(AreaOfInterest, AreaOfInterestLowered),
   12           sub_atom(AboutLowered, _, _, _, AreaOfInterestLowered)
   13        ;  false
   14        )
   15    ;
   16        (LanguageParadigm \= '', Paradigm \= ''
   17        -> downcase_atom(LanguageParadigm, LanguageParadigmLowered),
   18           downcase_atom(Paradigm, ParadigmLowered),
   19           sub_atom(LanguageParadigmLowered, _, _, _, ParadigmLowered)
   20        ;  false
   21        )
   22    ;
   23        (LanguageTypes \= '', TypeSystem \= ''
   24        -> downcase_atom(LanguageTypes, LanguageTypesLowered),
   25           downcase_atom(TypeSystem, TypeSystemLowered),
   26           sub_atom(LanguageTypesLowered, _, _, _, TypeSystemLowered)
   27        ;  false
   28        )
   29    ).
   30  
   31search_languages(AreaOfInterest, Paradigm, TypeSystem, LanguagesFound) :-
   32    get_languages(Languages),
   33    filter_languages(
   34        Languages, 
   35        AreaOfInterest, 
   36        Paradigm, 
   37        TypeSystem, 
   38        LanguagesFound).
   39
   40filter_languages([], _, _, _, []).
   41filter_languages(
   42        [Language | RestLanguages], 
   43        AreaOfInterest, 
   44        Paradigm, 
   45        TypeSystem, 
   46        LanguagesFound) :-
   47    match_language(Language, AreaOfInterest, Paradigm, TypeSystem),
   48    filter_languages(
   49        RestLanguages, 
   50        AreaOfInterest, 
   51        Paradigm, 
   52        TypeSystem,
   53        RestLanguagesFound),
   54    LanguagesFound = [Language | RestLanguagesFound].
   55filter_languages(
   56        [Language | RestLanguages], 
   57        AreaOfInterest, 
   58        Paradigm, 
   59        TypeSystem, 
   60        LanguagesFound) :-
   61    not(match_language(Language, AreaOfInterest, Paradigm, TypeSystem)),
   62    filter_languages(
   63        RestLanguages, 
   64        AreaOfInterest, 
   65        Paradigm, 
   66        TypeSystem,
   67        RestLanguagesFound),
   68    LanguagesFound = RestLanguagesFound.
   69
   70search_by_name(NameToSearch, [Language | _ ], LanguageFound) :-
   71    NameToSearch \= '',
   72    language(Name, _, _, _, _, _, _) = Language,
   73    downcase_atom(NameToSearch, NameToSearchLowered),
   74    downcase_atom(Name, NameLowered),
   75    sub_atom(NameLowered, _, _, _, NameToSearchLowered),
   76    Language = LanguageFound.
   77search_by_name(NameToSearch, [Language | RestLanguages], LanguageFound) :-
   78    NameToSearch \= '',
   79    language(Name, _, _, _, _, _, _) = Language,
   80    downcase_atom(NameToSearch, NameToSearchLowered),
   81    downcase_atom(Name, NameLowered),
   82    not(sub_atom(NameLowered, _, _, _, NameToSearchLowered)),
   83    search_by_name(NameToSearch, RestLanguages, LanguageFound)