Assignment 1 Variable Allocation Register Allocation Sorted List Knowledge Base /* define the allocations of variables to registers */ allocateVariable(v,a). allocateVariable(w,b). allocateVariable(x,c). allocateVariable(y,a). allocateVariable(z,b). /* define the variables that are in use together */ conflict(v,w). conflict(w,x). conflict(w,y). conflict(x,y). conflict(y,z). /* define the fact that variables that are in use together cannot share a register */ valid(Q1,Q2) :- allocateVariable(Q1,X), allocateVariable(Q2,Y), X\=Y. /* check that all variables that are in use together do not share a register */ validRegisterAllocation :- forall(conflict(X,Y),valid(X,Y)). Queries validRegisterAllocation. Run Output Knowledge Base /* define the registers we are allowed to use */ register(a). register(b). register(c). /* calling validRegisterAllocation(V,W,X,Y,Z) and iterating through gives all valid combinations (of which there are 24) */ validRegisterAllocation(RegV,RegW,RegX,RegY,RegZ) :- register(RegV), register(RegW), register(RegX), register(RegY), register(RegZ), RegV\=RegW, RegW\=RegX, RegW\=RegY, RegX\=RegY, RegY\=RegZ. Queries validRegisterAllocation(V,W,X,Y,Z). Run Output Knowledge Base /* the empty list is always sorted */ sorted([]). /* a list of a single element (the tail is empty) is also always sorted */ %Improved by me, although not needed. -CM sorted([T]). %sorted([_|T]) :- % T=[]. /* a list is sorted if the tail is sorted and the head is the smallest element of the list */ sorted([H|T]) :- min_list([H|T], H), sorted(T). Queries sorted([1,2,3,4,5]). sorted([1,1,2,2,3]). sorted([1,2,3,2,1]). Run Output