Advertisement
Guest User

Untitled

a guest
Jan 23rd, 2018
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 15.05 KB | None | 0 0
  1. #|<html><head></head><body><pre style="word-wrap: break-word; white-space: pre-wrap;">#|
  2.  
  3. CS 2800 Homework 2 - Spring 2018
  4.  
  5. This homework is done in groups.
  6.  
  7. * Groups should consist of 2-3 people.
  8.  
  9. * One group member will create a group in BlackBoard. See the class Webpage for
  10. instructions on how to do that.
  11.  
  12. * Other group members then join the group.
  13.  
  14. * Homework is submitted by only one person per group. Therefore make sure the
  15. person responsible for submitting actually does so. If that person forgets to
  16. submit, your team gets 0.
  17.  
  18. - We recommend that groups email confirmation messages and submit early and
  19. often.
  20.  
  21. * Submit the homework file (this file) on Blackboard. Do not rename
  22. this file. There will be a 10 point penalty for this.
  23.  
  24. * You must list the names of ALL group members below, using the given
  25. format. This way we can confirm group membership with the BB groups. If you
  26. fail to follow these instructions, it costs us time and it will cost you
  27. points, so please read carefully.
  28.  
  29. The format should be: FirstName1 LastName1, FirstName2 LastName2,
  30. For example:
  31. Names of ALL group members: Frank Sinatra, Billy Holiday
  32.  
  33. There will be a 10 pt penalty if your names do not follow this format.
  34.  
  35. Replace "" below with the names as explained above.
  36.  
  37. Names of ALL group members:
  38.  
  39. * Later in the term if you want to change groups, the person who created the
  40. group should stay in the group. Other people can leave and create other groups
  41. or change memberships. See the class Webpage.
  42. |#
  43. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  44.  
  45. For this homework you will need to use ACL2s.
  46.  
  47. Technical instructions:
  48.  
  49. - Open this file in ACL2s as hwk2.lisp
  50.  
  51. - Make sure you are in BEGINNER mode. This is essential! Note that you can
  52. only change the mode when the session is not running, so set the correct
  53. mode before starting the session.
  54.  
  55. - Insert your solutions into this file where indicated (usually as "")
  56.  
  57. - Only add to the file. Do not remove or comment out anything pre-existing.
  58.  
  59. - Make sure the entire file is accepted by ACL2s. In particular, there must
  60. be no "" left in the code. If you don't finish all problems, comment
  61. the unfinished ones out. Comments should also be used for any English
  62. text that you may add. This file already contains many comments, so you
  63. can see what the syntax is.
  64.  
  65. - When done, save your file and submit it as hwk2.lisp
  66.  
  67. - Do not submit the session file (which shows your interaction with the theorem
  68. prover). This is not part of your solution. Only submit the lisp file.
  69.  
  70. Instructions for programming problems:
  71.  
  72. For each function definition, you must provide both contracts and a body.
  73.  
  74. You must also ALWAYS supply your own tests. This is in addition to the
  75. tests sometimes provided. Make sure you produce sufficiently many new test
  76. cases. This means: cover at least the possible scenarios according to the
  77. data definitions of the involved types. For example, a function taking two
  78. lists should have at least 4 tests: all combinations of each list being
  79. empty and non-empty.
  80.  
  81. Beyond that, the number of tests should reflect the difficulty of the
  82. function. For very simple ones, the above coverage of the data definition
  83. cases may be sufficient. For complex functions with numerical output, you
  84. want to test whether it produces the correct output on a reasonable
  85. number of inputs.
  86.  
  87. Use good judgment. For unreasonably few test cases we will deduct points.
  88.  
  89. We will use ACL2s' check= function for tests. This is a two-argument
  90. function that rejects two inputs that do not evaluate equal. You can think
  91. of check= roughly as defined like this:
  92.  
  93. (defunc check= (x y)
  94. :input-contract (equal x y)
  95. :output-contract (equal (check= x y) t)
  96. t)
  97.  
  98. That is, check= only accepts two inputs with equal value. For such inputs, t
  99. (or "pass") is returned. For other inputs, you get an error. If any check=
  100. test in your file does not pass, your file will be rejected.
  101.  
  102. Since this is our first programming exercise, we will simplify the interaction
  103. with ACL2s somewhat. Instead of requiring ACL2s to prove termination and
  104. contracts, we allow ACL2s to proceed even if a proof fails. However, if a
  105. counterexample is found, ACL2s will report it. See section 2.17 of the lecture
  106. notes for more information. This is achieved using the following directives (do
  107. not remove them):
  108.  
  109. |#
  110.  
  111. (set-defunc-termination-strictp nil)
  112. (set-defunc-function-contract-strictp nil)
  113. (set-defunc-body-contracts-strictp nil)
  114.  
  115. #|
  116.  
  117. Part I:
  118. Function definitions.
  119.  
  120. In this part, you will be given a set of programming exercises. Since you
  121. already took a fantastic course on programming (CS2500), the problems are not
  122. entirely trivial. Make sure you give yourselves enough time to develop
  123. solutions and feel free to define helper functions as needed.
  124.  
  125. |#
  126.  
  127. #|
  128. Define the function rr.
  129. rr: List x Nat -&gt; List
  130.  
  131. (rr l n) rotates the list l to the right n times.
  132. |#
  133.  
  134. ;(defunc rr (l n)
  135. ; :input-contract
  136. ;:output-contract
  137. ;)
  138.  
  139. ;(check= (rr '(1 2 3) 1) '(3 1 2))
  140. ;(check= (rr '(1 2 3) 2) '(2 3 1))
  141. ;(check= (rr '(1 2 3) 3) '(1 2 3))
  142.  
  143. #|
  144. Define the function err, an efficient version of rr.
  145. err: List x Nat -&gt; List
  146.  
  147. (err l n) returns the list obtained by rotating l to the right n times
  148. but it does this efficiently because it actually never rotates more than
  149. (len l) times.
  150. You can use acl2::mod in your answer. For example (acl2::mod 11 3) = 2
  151. because 11 mod 3 = 2.
  152. |#
  153.  
  154. #| (defunc err (l n)
  155. :input-contract
  156. :output-contract
  157. )
  158. |#
  159. #|
  160. (check= (err '(1 2 3) 1) '(3 1 2))
  161. (check= (err '(1 2 3) 2) '(2 3 1))
  162. (check= (err '(1 2 3) 3) '(1 2 3))
  163. |#
  164. #|
  165. Make sure that err is efficient by timing it with a large n
  166. and comparing the time with rr.
  167.  
  168. Replace the 's in the string below with the times you
  169. observed.
  170. |#
  171. #|
  172. (acl2::time$ (rr '(a b c d e f g) 10000000))
  173. (acl2::time$ (err '(a b c d e f g) 10000000))
  174.  
  175. "rr took seconds but err took seconds"
  176. |#
  177. ;; Here is a data definition for a list of Booleans. See Section 2.14 of the
  178. ;; lecture notes.
  179.  
  180. (defdata lob (listof boolean))
  181.  
  182. ;; For example
  183.  
  184. (check= (lobp '(t nil t)) t)
  185. (check= (lobp '(nil 1 t)) nil)
  186.  
  187. #|
  188. We can use list of Booleans to represent natural numbers as follows.
  189. The list
  190.  
  191. (nil nil t)
  192.  
  193. corresponds to the number 4 because the first nil is the "low-order"
  194. bit of the number which means it corresponds to 2^0=1 if t and 0
  195. otherwise. The next Boolean corresponds to 2^1=2 if t and 0 otherwise
  196. and so on. So the above number is:
  197.  
  198. 0 + 0 + 2^2 = 4.
  199.  
  200. As another example, 31 is
  201.  
  202. (t t t t t)
  203.  
  204. or
  205.  
  206. (t t t t t nil nil)
  207.  
  208. or
  209.  
  210. Define the function n-to-b that given a natural number, returns a list of
  211. Booleans, of minimal length, corresponding to that number.
  212. |#
  213.  
  214. (defunc n-to-b (n)
  215. :input-contract (natp n)
  216. :output-contract (lobp (n-to-b n))
  217. (cond
  218. ((equal n 0) ())
  219. ((equal n 1) (cons t ()))
  220. ((equal (acl2::mod n 2) 0) (cons nil (n-to-b (/ n 2))))
  221. (t (cons t (n-to-b (/ (- n 1) 2))))))
  222.  
  223. (check= (n-to-b 24) '(nil nil nil t t))
  224. (check= (n-to-b 10) '(nil t nil t))
  225. (check= (n-to-b 7) '(t t t))
  226. (check= (n-to-b 4) '(nil nil t))
  227. (check= (n-to-b 31) '(t t t t t))
  228. (check= (n-to-b 0) ())
  229.  
  230. #|
  231. Define the function b-to-n that given a list of Booleans, returns
  232. the natural number corresponding to that list.
  233. |#
  234.  
  235. (defunc b-to-n (l)
  236. :input-contract (lobp l)
  237. :output-contract (natp (b-to-n l))
  238. (cond
  239. ((endp l) 0)
  240. ((equal (first l) t) (+ 1 (* 2 (b-to-n (rest l)))))
  241. ((equal (first l) nil) (* 2 (b-to-n (rest l))))))
  242.  
  243.  
  244. (check= (b-to-n '(nil t nil t)) 10)
  245. (check= (b-to-n '(t t t)) 7)
  246. (check= (b-to-n '(nil nil t)) 4)
  247. (check= (b-to-n '(t t t t t)) 31)
  248. (check= (b-to-n ()) 0)
  249.  
  250. #|
  251.  
  252. The permutations of a list are all the list you can obtain by swapping any two
  253. of its elements. For example, starting with the list
  254.  
  255. (1 2 3)
  256.  
  257. I can obtain
  258.  
  259. (3 2 1)
  260.  
  261. by swapping 1 and 3.
  262.  
  263. So the permutations of (1 2 3) are
  264.  
  265. (1 2 3) (1 3 2) (2 1 3) (2 3 1) (3 1 2) (3 2 1)
  266.  
  267. Notice that if the list is of length n and all of its elements are distinct, it
  268. has n! (n factorial) permutations.
  269.  
  270. Given a list, say (a b c d e), we can define any of its permutations using a
  271. list of *distinct* positive integers from 1 to the length of the list, which
  272. tell us how to reorder the elements of the list. Let us call this list of
  273. distinct positive integers an arrangement. For example applying the
  274. arrangement (5 1 3 2 4) to the list (a b c d e) yields (e a c b d).
  275.  
  276. Define the function find-arrangement that given two lists, either returns nil if
  277. they are not permutations of one another or returns an arrangement such that
  278. applying the arrangement to the first list yields the second list. Note that if
  279. the lists have repeated elements, then more than one arrangement will work. In
  280. such cases, it does not matter which of these arrangements you return.
  281.  
  282. |#
  283. (defunc member (a b)
  284. :input-contract (and t (listp b))
  285. :output-contract (booleanp (member a b))
  286. (cond
  287. ((endp b) nil)
  288. ((listp b) (if (equal a (first b)) t
  289. (member a (rest b))))))
  290.  
  291. (check= (member "h" '(a b c)) nil)
  292. (check= (member 'x '(x y z)) t)
  293. (check= (member () (cons () ())) t)
  294. (check= (member nil '(nil t nil)) t)
  295.  
  296.  
  297. (defunc remover (a b)
  298. :input-contract (and t (listp b))
  299. :output-contract (listp (remover a b))
  300. (cond
  301. ((endp b) b)
  302. (t (if (equal a (first b))
  303. (rest b)
  304. (cons (first b) (remover a (rest b)))))))
  305.  
  306. (check= (remover 1 '(1 2 3)) '(2 3))
  307. (check= (remover 5 '(4 1 5 8)) '(4 1 8))
  308. (check= (remover 8 '(1 2 3)) '(1 2 3))
  309.  
  310.  
  311.  
  312.  
  313.  
  314. (defunc permhelper (a b)
  315. :input-contract (and (listp a) (listp b))
  316. :output-contract (booleanp (permhelper a b))
  317. (cond
  318. ((not (equal (len a) (len b))) nil)
  319. ((and (endp a) (endp b)) t)
  320. ((or (endp a) (endp b)) nil)
  321. ((endp (rest a)) (member (first a) b))
  322. (t
  323. (if
  324. (member (first a) b)
  325. (permhelper (rest a) (remover (first a) b))
  326. nil))))
  327.  
  328. (check= (permhelper '(1 2 3) '(2 3 1)) t)
  329. (check= (permhelper '(5 1 3) '(2 3 1)) nil)
  330. (check= (permhelper '(5 1 3 6 7) '(2 3 1 1)) nil)
  331. (check= (permhelper () '(2 3 1)) nil)
  332. (check= (permhelper '(1 2 3) '(1 2 3 4)) nil)
  333. (check= (permhelper '(1 2 3 4) '(1 2 3)) nil)
  334.  
  335. (defunc replacer (x y)
  336. :input-contract (and t (listp y))
  337. :output-contract (listp y)
  338. (cond
  339. ((not (member x y)) y)
  340. ((equal x (first y)) (cons () (rest y)))
  341. (t (cons (first y)(replacer x (rest y))))))
  342.  
  343. (check= (replacer 0 '(1 2 3)) '(1 2 3))
  344. (check= (replacer 0 '(1 0 2)) '(1 () 2))
  345. (check= (replacer 0 '(0 1 2)) '(() 1 2))
  346. (check= (replacer 0 '(0 1 0)) '(() 1 0))
  347. (check= (replacer 1 '(0 1 0)) '(0 () 0))
  348. (check= (replacer 5 '(3 1 5)) '(3 1 ()))
  349.  
  350. (defunc positionhelper (x y)
  351. :input-contract (and t (listp y))
  352. :output-contract (natp (positionhelper x y))
  353. (cond
  354. ((endp y) 0)
  355. ((equal x (first y)) 1)
  356. ((not (member x y)) 0)
  357. (t (+ 1 (positionhelper x (rest y))))))
  358.  
  359. (check= (positionhelper 1 '(1 2 3)) 1)
  360. (check= (positionhelper 1 ()) 0)
  361. (check= (positionhelper 2 '(3 1 2)) 3)
  362.  
  363. (defunc generate (x y)
  364. :input-contract (and (listp x) (listp y))
  365. :output-contract (listp (generate x y))
  366. (cond
  367. ((endp x) ())
  368. (t (cons (positionhelper (first x) y) (generate (rest x) (replacer (first x) y))))))
  369.  
  370. (check= (generate '(a b c) '(a c b)) '(1 3 2))
  371. (check= (generate () '(a a)) ())
  372.  
  373. (defunc find-arrangement (x y)
  374. :input-contract (and (listp x) (listp y))
  375. :output-contract (listp (find-arrangement x y))
  376. (cond
  377. ((permhelper x y) (generate x y))
  378. (t nil)))
  379.  
  380.  
  381.  
  382.  
  383.  
  384. (check= (find-arrangement '(a b c) '(a b b)) nil)
  385. (check= (find-arrangement '(a b c) '(a c b)) '(1 3 2))
  386. (check= (find-arrangement '(a a) '(a a)) '(1 2))
  387. ;; in the above check= you can use '(2 1) instead of '(1 2) if you wish
  388. ;; since both arrangements work
  389.  
  390.  
  391. #|
  392.  
  393. Part II:
  394. Exploring the ACL2s language.
  395.  
  396. In this section, we are going to explore the ACL2s language as defined in the
  397. lecture notes. We are going to revisit some of the decisions made and we will
  398. consider certain "what if" scenarios. Make sure you read and understand the
  399. lecture notes really well.
  400.  
  401. |#
  402.  
  403. #|
  404.  
  405. The built-in functions introduced in Section 2.2 are if and equal.
  406. Without using if, but using equal, booleanp and the constants, which
  407. of the following functions can you define?
  408.  
  409. and, implies, or, not, iff, xor
  410.  
  411. For each of the functions you can define, provide a definition using
  412. the names
  413.  
  414. band, bimplies, bor, bnot, biff, bxor
  415.  
  416. If you cannot define one of the above functions, you do not need to do
  417. anything.
  418.  
  419. The restriction on not using if applies only to the body of your function
  420. definitions. You can use all the functions in the lecture notes (including
  421. booleanp, if and and) for the input and output contracts.
  422.  
  423. |#
  424.  
  425.  
  426.  
  427.  
  428. #|
  429. Consider the following function definition.
  430. |#
  431.  
  432. (defunc iif (a b c)
  433. :input-contract (booleanp a)
  434. :output-contract t
  435. (if a b c))
  436.  
  437. #|
  438. This is accepted by ACL2s. Suppose I define iif. Is it the case
  439. that I can always use iif instead of if for subsequent function
  440. definitions? Explain why or why not.
  441.  
  442. (replace this line with your explanation, in English)
  443.  
  444. |#
  445.  
  446. #|
  447. In the lecture notes, unary-- was provided as a built-in function.
  448. Explain why it is required or show that it is not really required.
  449.  
  450. When we say that unary-- is required, we mean that you cannot define
  451. it in the ACL2s language (Bare Bones mode) using the built-in
  452. functions in the lecture notes upto the end of Section 2.5, except
  453. that you cannot use unary-- (obviously). You can define helper
  454. functions, if needed.
  455.  
  456. If you think it is really required, then argue that it allows you to
  457. do things that the other built-in functions do not. You do not need a
  458. proof here, just a good argument in English.
  459.  
  460. If you think it is not required, show how to define it using the other
  461. built-in functions. Do this by providing a definition of my-unary--
  462. that only uses the other built-in functions (except unary--) or
  463. functions defined based on the other built-in functions.
  464. You will do this in Beginner mode, as per the instructions above.
  465.  
  466. |#
  467.  
  468. (the definition of my-unary-- goes here or a comment with an argument)
  469.  
  470. #|
  471.  
  472. In the lecture notes, unary-/ was provided as a built-in function.
  473. Explain why it is required or show that it is not really required.
  474. Follow the instructions for unary--.
  475.  
  476. |#
  477.  
  478. (the definition of my-unary-/ goes here or a comment with an argument)
  479.  
  480. #|
  481.  
  482. In the lecture notes, integerp was provided as a built-in function.
  483. Explain why it is required or show that it is not really required.
  484. Follow the instructions for unary--.
  485.  
  486. |#
  487.  
  488. (the definition of my-integerp goes here or a comment with an argument)
  489.  
  490. #|
  491. In the lecture notes, denominator was provided as a built-in function.
  492. Explain why it is required or show that it is not really required.
  493. Follow the instructions for unary--.
  494. |#
  495.  
  496. (the definition of denominator goes here or a comment with an argument)
  497.  
  498. </pre></body></html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement