Hi There!

I'm Dan Schlegel, an Associate Professor in the Computer Science Department at SUNY Oswego

Logic Problem Solving in Prolog

% Help Grandma Iva write down some of her favorite recipes.
% Determine the baking temperature and time of each recipe.

% 1. The dish that cooks at 340 degrees bakes 5 minutes longer than the chicken cutlet.
% 2. The dish that cooks at 350 degrees is either the dish that bakes for 35 minutes or the meatloaf.
% 3. The mac and cheese bakes 5 minutes longer than the recipe that cooks at 340 degrees.
% 4. The dish that bakes for 35 minutes cooks at 400 degrees.

% baking times: 25, 30, 35, 40 minutes
% temperatures: 340, 350, 375, 400
% recipes: candied yams, chicken cutlet, mac and cheese, meatloaf

solve(Dishes) :-
    Dishes = [dish(candied_yams,   TempY, TimeY),
              dish(chicken_cutlet, TempC, TimeC),
              dish(mac_and_cheese, TempM, TimeM),
              dish(meatloaf,       TempL, TimeL)],

    % Try every ordering of the temperatures and times
    % permutation([340, 350, 375, 400], [TempY, TempC, TempM, TempL]),
    % permutation([25, 30, 35, 40],     [TimeY, TimeC, TimeM, TimeL]),

    % Pick a temperature for each dish, then make sure none repeat
    member(TempY, [340, 350, 375, 400]),
    member(TempC, [340, 350, 375, 400]),
    member(TempM, [340, 350, 375, 400]),
    member(TempL, [340, 350, 375, 400]),
    is_set([TempY, TempC, TempM, TempL]),

    % Same for the times
    member(TimeY, [25, 30, 35, 40]),
    member(TimeC, [25, 30, 35, 40]),
    member(TimeM, [25, 30, 35, 40]),
    member(TimeL, [25, 30, 35, 40]),
    is_set([TimeY, TimeC, TimeM, TimeL]),

    % 1. The 340 dish bakes 5 minutes longer than the chicken cutlet
    member(dish(_, 340, Time340), Dishes),
    Time340 =:= TimeC + 5,

    % 2. The 350 dish is either the 35-minute dish or the meatloaf
    member(dish(Name350, 350, Time350), Dishes),
    ( Time350 =:= 35 ; Name350 == meatloaf ),

    % 3. The mac and cheese bakes 5 minutes longer than the 340 dish
    TimeM =:= Time340 + 5,

    % 4. The 35-minute dish cooks at 400 degrees
    member(dish(_, 400, 35), Dishes).

show :-
    solve(Dishes),
    forall(member(dish(Name, Temp, Time), Dishes),
           format("~w: ~w degrees, ~w minutes~n", [Name, Temp, Time])).