Courses/CS 460/Fall 2005/Homework/Brian Smith/Oct 22

From CSWiki

Jump to: navigation, search

From Judy's Logic Problems.

Contents


[edit] Four Trees

Each of four men who live in Willowbrook Estates on the city’s north side, planted a different type of tree in their front yard on Arbor Day. From The clues, determine the first and last name of each man and the type of tree each planted.

  1. George, Mr.Clary, and Mr. Becker (who got his tree from HomeTown Garden Center) all live within three blocks of one another.
  2. Mr. Clary planted neither the cedar tree or the maple tree.
  3. Harvey and Mr. Clary live next door to one another.
  4. The ash tree was not planted by neither John nor Ivan.
  5. The maple tree was not the tree planted by George (who is not surnamed Delgado).
  6. Mr. Erichsen isn’t the man who planted the ash tree.
  7. John, who purchased his tree at the Green Thumb Greenhouse, isn’t the man who planted the sycamore.

local
   FirstNames = [george harvey ivan john]
   SurNames = [becker clary delgado erichsen]
   Trees = [ash cedar maple sycamore]

   %----- Helper Procedures -----

   proc {Append ?Xs ?Ys ?Zs}
      choice
	 Xs = nil
	 Ys = Zs
      [] Head XRest ZRest in
	 Xs = Head|XRest 
	 Zs = Head|ZRest
	 {Append XRest Ys ZRest}
      end
   end
   proc {IsAFirstName ?F} {IsAMember F FirstNames} end
   proc {IsAMember ?X Xs} {Append _ X|_ Xs} end
   proc {IsAPropertyValue Results ?Property ?Value}
      {IsAMember Property#Value {Record.toListInd Results}}
   end

   %----- Clues -----
   % Try to assign first names to last names, and first names to trees.
   % The helper procedures use IsAMember to provide a choice point, so each
   % clue tries to assign all possible choices. Some fail based on the
   % restraints in each clue (failed unifications).

   % 1.
   % george \= clary
   % george \= becker
   % clary \= becker
   proc {Clue1 Results}
      F1 F2 in
      {IsAFirstName F1}
      {IsAFirstName F2}
      F1 == george = false
      F2 == george = false
      F1 == F2 = false
      {IsAPropertyValue Results becker F1}
      {IsAPropertyValue Results clary F2}
   end

   % 2.
   % clary \= cedar
   % clary \= maple
   proc {Clue2 Results}
      F1 F2 in
      {IsAFirstName F1}
      {IsAFirstName F2}
      F1 == Results.clary = false
      F2 == Results.clary = false
      F1 == F2 = false
      {IsAPropertyValue Results cedar F1}
      {IsAPropertyValue Results maple F2}
   end

   % 3.
   % harvey \= clary
   proc {Clue3 Results}
      Results.clary == harvey = false
   end

   % 4.
   % ash \= john
   % ash \= ivan
   proc {Clue4 Results}
      F in
      {IsAFirstName F}
      F == john = false
      F == ivan = false
      F == Results.cedar = false
      F == Results.maple = false
      {IsAPropertyValue Results ash F}
   end

   % 5.
   % maple \= george
   % george \= delgado
   proc {Clue5 Results}
      F in
      Results.maple == george = false
      {IsAFirstName F}
      F == george = false
      F == Results.becker = false
      F == Results.clary = false
      {IsAPropertyValue Results delgado F}
   end

   % 6.
   % erichsen \= ash
   proc {Clue6 Results}
      F in
      {IsAFirstName F}
      F == Results.ash = false
      F == Results.becker = false
      F == Results.clary = false
      F == Results.delgado = false
      {IsAPropertyValue Results erichsen F}
   end

   % 7.
   % john \= becker
   % john \= sycamore
   proc {Clue7 Results}
      F in
      Results.becker == john = false
      {IsAFirstName F}
      F == john = false
      F == Results.ash = false
      F == Results.cedar = false
      F == Results.maple = false
      {IsAPropertyValue Results sycamore F}
   end

   %----- Execution -----

   fun {FourTrees}
      Properties = {FoldR [SurNames Trees] Append nil}
      Results = {MakeRecord resultProperties Properties}
   in
      {Clue1 Results}
      {Clue2 Results}
      {Clue3 Results}
      {Clue4 Results}
      {Clue5 Results}
      {Clue6 Results}
      {Clue7 Results}
      Results
   end
in
   {Browse {SearchAll FourTrees}}
end

[edit] Flower Boxes

Mary and three other women had flower boxes in windows on either side of their front doors and each woman planted a different type of flower in her window flower boxes (one woman planted petunias). From the clues, can you determine the first name and last name (one is Gumble) of each woman and the type of flower each woman planted?

  1. The four women - Julie, Ms. Drake, Ms. Evans, and the woman who planted daisies, all belong to the same gardening club.
  2. The woman who planted violets in her flower boxes isn’t Kate.
  3. Ms. Drake isn’t the woman who planted marigolds.
  4. Ms. Florez, who didn’t plant violets, is married to Lola’s brother.
  5. Marigolds were not the flower of choice for Julie.
  6. Kate, whose surname isn’t Evans, didn’t plant daisies in her window flower boxes.
local
   FirstNames = [julie kate lola mary]
   SurNames = [drake evans florez gumble]
   Flowers = [daisies marigolds petunias violets]

   %----- Helper Procedures -----

   proc {Append ?Xs ?Ys ?Zs}
      choice
	 Xs = nil
	 Ys = Zs
      [] Head XRest ZRest in
	 Xs = Head|XRest 
	 Zs = Head|ZRest
	 {Append XRest Ys ZRest}
      end
   end
   proc {IsAFirstName ?F} {IsAMember F FirstNames} end
   proc {IsAMember ?X Xs} {Append _ X|_ Xs} end
   proc {IsAPropertyValue Results ?Property ?Value}
      {IsAMember Property#Value {Record.toListInd Results}}
   end

   %----- Clues -----
   % Try to assign first names to last names, and first names to flowers.
   % The helper procedures use IsAMember to provide a choice point, so each
   % clue tries to assign all possible choices. Some fail based on the
   % restraints in each clue (failed unifications).

   % 1.
   % drake \= julie
   % evans \= julie
   % daisies \= julie
   % drake \= daisies
   % evans \= daisies
   proc {Clue1 Results}
      F1 F2 F3 in
      {IsAFirstName F1}
      {IsAFirstName F2}
      {IsAFirstName F3}
      F1 == julie = false
      F2 == julie = false
      F3 == julie = false
      F1 == F2 = false
      F2 == F3 = false
      F1 == F3 = false
      {IsAPropertyValue Results drake F1}
      {IsAPropertyValue Results evans F2}
      {IsAPropertyValue Results daisies F3}
   end
   
   % 2.
   % violets \= kate
   proc {Clue2 Results}
      F in
      {IsAFirstName F}
      F == kate = false
      F == Results.daisies = false
      {IsAPropertyValue Results violets F}
   end
   
   % 3.
   % drake \= marigolds
   proc {Clue3 Results}
      F in
      {IsAFirstName F}
      F == Results.drake = false
      F == Results.daisies = false
      F == Results.violets = false
      {IsAPropertyValue Results marigolds F}
   end
   
   % 4.
   % florez \= violets
   % florez \= lola
   proc {Clue4 Results}
      F in
      {IsAFirstName F}
      F == lola = false
      F == Results.violets = false
      F == Results.drake = false
      F == Results.evans = false
      {IsAPropertyValue Results florez F}
   end
   
   % 5.
   % marigolds \= julie
   proc {Clue5 Results}
      Results.marigolds == julie = false
   end
   
   % 6.
   % evans \= kate
   % daisies \= kate
   proc {Clue6 Results}
      Results.evans == kate = false
      Results.daisies == kate = false
   end

   % Cleanup
   % Some properties are not explicitly set in the clues. I run this proc
   % after the clues to insert whatever values are left.
   proc {Remainder Results}
      F1 F2 in
      % assign a first name to Gumble
      {IsAFirstName F1}
      F1 == Results.drake = false
      F1 == Results.evans = false
      F1 == Results.florez = false
      {IsAPropertyValue Results gumble F1}
      % assign a first name to petunias
      {IsAFirstName F2}
      F2 == Results.daisies = false
      F2 == Results.marigolds = false
      F2 == Results.violets = false
      {IsAPropertyValue Results petunias F2}
   end

   %----- Execution -----

   fun {FlowerBoxes}
      Properties = {FoldR [SurNames Flowers] Append nil}
      Results = {MakeRecord resultProperties Properties}
   in
      {Clue1 Results}
      {Clue2 Results}
      {Clue3 Results}
      {Clue4 Results}
      {Clue5 Results}
      {Clue6 Results}
      {Remainder Results}
      Results
   end
in
   {Browse {SearchAll FlowerBoxes}}
end