CSE 341 - Homework 9 - CLP(R)

Due: Dec 6, 10pm. Maximum of 3 late days for this assignment (even if you have more than that left), so that we can post a sample solution in time for students to use in studying for the final.

10 points total, plus up to 3 points extra credit

  1. Write a CLP(R) rule abs for finding the absolute value of a number. Demonstrate your rule where both arguments are constants, just one is a constant, and both are variables. In each case backtrack to find all possible solutions. For example, one of your test goals might be abs(X,10). You should get two solutions:
    X = 10
    X = -10
    
  2. Write a rule repeat that defines the repeat relation among its arguments X, N, and S. The relation holds if S is a list consisting of N occurrences of X. For example
    repeat(squid,4,[squid,squid,squid,squid])
    should succeed. The goal
    repeat(clam,3,L)
    should succeed with
    L = [clam,clam,clam]
    (and no other possibilities). Also try repeat(squid,N,L) and repeat(X,N,L).

    Hint: if you get a message ``Fatal Error: Trail overflow'' for any of the possibilities, you should put in an additional condition on one of your rules. (Are you searching for a list with a negative length?)

  3. Write a rule minimum that finds the minimum element in a list of numbers. For example, minimum([4,2,6,2],N) should succeed once with N = 2. The goal minimum([],N) should fail, since there is no minimum element in the empty list. The goal minimum(L,4) should succeed as many times as you try it, binding L to various lists of which 4 is the minimum element. (These don't need to be generated in any particular order.)
  4. The remaining questions build on the file resistors.clpr. Copy this file to your own directory on attu using this command:
    cp ~borning/clpr/resistors.clpr .
    You shouldn't need to modify this file. You can then read in both the resistors file and your own additions (say in a file hw9.clpr) using this command in CLP(R):
     [resistors, hw9].

    Define one or more rules that define how a switch operates. The rule(s) should take 3 arguments: 2 leads, and a position. The position will be either open or closed, and your switch should define the appropriate constraints depending on the position.

    Using the switch and the other rules in resistors.clpr you can define a simple_circuit rule that builds the following circuit. This rule should have two arguments: the state of the switch, and the amperes shown by the ammeter.

    To help you get started here's a definition:

    simple_circuit(State,Amps) :-
       battery(B1,B2,10),
       ammeter(A1,A2,Amps),
       switch(S1,S2,State),
       resistor(R1,R2,100),
       electrical_ground(G),
       connect([B2,A1]), connect([A2,S1]), connect([S2,R1]),
       connect([R2,B1,G]).
    
    Try invoking simple_circuit with the switch open, and then closed; and also with the switch state left as a variable.
  5. Define another rule complex_circuit that builds the following curcuit. This rule should have six arguments: one for the amperes shown on the ammeter, and five for the states of the switches S1 through S5.

    Use this rule to find the current when S1 is open and the other switches are closed. Also use the rule to find all settings for the switches such that the current is at most 0.04 amperes. Finally, if you closed all the switches for this circuit with a real device, something bad would probably happen. What happens with the CLP(R) program? Why? (Put your answer to this question in a comment.)

Turnin

Turn in two files: your CLPR program (say hw9.clpr), and a transcript showing tests of your rules. Please put your name at the top of each file (as a comment in the source code, and also in the transcript). To make this easier to grade, don't include the contents of resistors.clpr in hw9.clpr -- leave it as a separate file. You can produce a transcript using the unix script command. The text you type is after the attu: prompts. (^d means hold down the control key and type d).
attu% script hw9-transcript
attu% echo "Name: <your name here>"
Name: <your name here>
attu% clpr
   ......
attu% ^d
Script done, output file is hw9-transcript

Extra Credit

  1. Define one or more rules for an SPDT switch ("single pole double throw"). This switch has a center terminal, and a top and bottom terminal. If its state is "up" the center terminal is connected to the top terminal, and if its state is "down" the center terminal is connected to the bottom terminal. Define your SPDT switch in terms of the basic SPST ("single pole single throw") switch you defined for questions 4 and 5, rather than by going to constraints directly on the voltage and current. (The idea with asking you to define the SPDT switch in terms of two SPST switches is that this is a very simple example of building larger components out of sub-components.)

    Then use the SPDT switch to build and test this circuit:

  2. Make a more elaborate version of the complex_circuit rule that allows N resistor/switch pairs rather than the three pairs in Question 5, where N is an argument to the rule. (The three pairs are S2, S3, and S4, and the accompanying 100, 200, and 300 ohm resistors.) A fourth resistor should be 400 ohms, a fifth one 500 ohms, and so forth.
  3. Define rules for additional sorts of electronic components, such as diodes or transistors. Show their operation in some suitable circuits.
  4. Extra credit problem 3 is kind of CompE oriented ... so to give equal time to CS students, here's another possibility. Look at the program
    ~borning/clpr/ibm-progs/smm
    This solves the cryptarithemtic puzzle SEND+MORE=MONEY. Adapt this to solve DOUBLE+DOUBLE+TOIL=TROUBLE.