Unformatted text preview:

Fall 2025 Programming Languages Homework 4 Solutions This homework is a combination programming and paper pencil assignment Due via Brightspace on Sunday November 30 2025 11 59 PM Eastern Standard Time Due to tim ing considerations relating to the end of the semester late submissions will not be accepted No exceptions For the Prolog questions you should use SWI Prolog Link is available on the course page For the Java questions optional we recommend Java 8 or higher You may collaborate and use any reference materials necessary to the extent required to understand the material All solutions must be your own except that you may rely upon and use Prolog code from the lecture if you wish Homework submissions containing any answers or code copied from any other source in part or in whole will receive a zero score and be subject to disciplinary action For all other questions Q3 Q5 ans Q6 a PDF file hw4 netid sols pdf containing the answers Q7 and Q8 are for study purposes only and should not be turned in Please submit your homework as a zip file hw4 netid zip The zip file should contain For Q1 hw4 netid Q1 pl which should contain the reordered facts for question 1 and the 2 new rules for sub question 4 For the Prolog Rules question Q2 hw4 netid Q2 pl which should contain your implemen tation of all of the rules You do not need to submit queries although we will run our queries on your solutions For Q4 hw4 netid Q4 pl which should contain all rules necessary to make the solution work Use comments to mark your modifications Make sure your Prolog code compiles before submitting If your Prolog code does not compile for any reason it may not be graded 1 1 15 points Investigating Prolog Consider the following user mode rules parent jason walt parent walt isabella parent becky walt parent becky bob parent dave bob parent lisa steve parent kent steve parent isabella joe parent steve joe parent isabella cathy parent norman cathy parent joe christian parent pebbles christian parent lisa sophia parent kent sophia parent sophia jack parent kev jack parent wanda isabella parent jesse walt parent jason suzanne parent jesse suzanne male dave male bob male jason male kent male steve male walt male norman male joe male christian male jack male kev female becky female lisa female pebbles female isabella female cathy female sophia female wanda female jesse female suzanne Using the rules and facts above write the following new rules in user mode Then show that they work properly in query mode by writing at least three queries for each demonstrating a true answer a true answer consisting of at least one variable instantiation which makes the query true and a false answer respectively You may use the relations shown in the lecture slides to answer these questions if you wish Page 2 You may also use the rules you are writing below to answer other rules below To save you some typing we have provided a file with this assignment that you can use to load in the facts above 1 Define new rule parents 3 which states that the first two arguments are parents of the third argument parents X Y Z parent X Z parent Y Z X Y 2 Define new rule sibling 2 that determines if 2 people are full siblings have the same two parents sibling X Y parent P1 X parent P1 Y parent P2 X parent P2 Y X Y P1 P2 3 Define new rule half sibling 2 that determines if 2 people are half siblings have exactly one half sibling X Y parent P X parent P Y X Y not parent P2 X parent P2 Y P2 P 4 Define new rule aunt 2 that determines if a person is the aunt of another person An aunt is defined as the sister of a person s parent aunt X Y parent P Y sibling X P female X parents dave becky bob true parents lisa kent X X steve parents norman isabella steve false sibling steve sophia true sibling X Y X steve Y sophia sibling bob walt false parent in common half sibling bob walt true half sibling bob X X walt half sibling steve sophia false aunt sophia joe true aunt X joe X sophia Page 3 5 Define new rule uncle 2 that determines if a person is the uncle of another person An uncle is defined as the brother of a person s parent uncle X Y parent P Y sibling X P male X 6 Define new rule niece 2 that determines if a person is the niece of another person A niece is defined as the female such that the other argument is her aunt or uncle aunt cathy christian false uncle X jack X steve uncle steve jack true uncle sophia joe false niece X Y female X uncle Y X niece X Y female X aunt Y X niece isabella suzanne true niece X Y X isabella Y suzanne X isabella Y suzanne false niece cathy wanda false nephew X Y male X uncle Y X nephew X Y male X aunt Y X nephew jack steve true nephew X Y X jack Y steve X jack Y steve X joe Y sophia X joe Y sophia Page 4 7 Define new rule nephew 2 that determines if a person is the nephew of another person A niece is defined as the male such that the other argument is his aunt or uncle nephew walt dave false 8 Modify the grandfather rule so that Prolog does not explore unnecessary alternative solutions Explain in your own words why the change affects the total execution time Show evidence of the faster execution time by providing a trace for each grandfather X Y male X parent X Z parent Z Y Page 5 2 25 points Prolog Rules Write the Prolog rules described below in a file hw4 netid prules pl All rules must be written using the subset of the Prolog language discussed in class You may not for example call built in library rules unless specifically covered in the slides You may call your own rules while formulating other rules You do not need to turn in queries although you should certainly test your rules using your own queries 1 Write a rule member positions X L Ps that finds all positions indices where element X ap pears in list L The output list Ps should contain these positions Example member positions 2 1 2 3 2 1 Ps Ps 2 4 member positions X L Ps find positions X L 1 Ps find positions find positions X X T Index Index Rest Next is Index 1 find positions X T Next Rest find positions X Y T Index Rest X Y Next is Index 1 find positions X T Next Rest Example is subset a c a b c d true is subset a c d a b false is subset is subset H T L is member H L is subset T L is …


View Full Document

NYU CSCI-GA 2110 - Homework 4 Solutions

Loading Unlocking...
Login

Join to view Homework 4 Solutions and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Homework 4 Solutions and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?