Advertisement
Guest User

Untitled

a guest
Oct 23rd, 2017
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 15.42 KB | None | 0 0
  1. #|
  2. CS 2800 Homework 7 - Fall 2017
  3.  
  4. This homework is to be done in a group of 2-3 students.
  5.  
  6. If your group does not already exist:
  7.  
  8. * One group member will create a group in BlackBoard
  9.  
  10. * Other group members then join the group
  11.  
  12. Submitting:
  13.  
  14. * Homework is submitted by one group member. Therefore make sure the person
  15. submitting actually does so. In previous terms when everyone needed
  16. to submit we regularly had one person forget but the other submissions
  17. meant the team did not get a zero. Now if you forget, your team gets 0.
  18. - It wouldn't be a bad idea for group members to send confirmation
  19. emails to each other to reduce anxiety.
  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.
  26. If you fail to follow these instructions, it costs us time and
  27. it will cost you points, so please read carefully.
  28.  
  29.  
  30. Names of ALL group members: FirstName1 LastName1, FirstName2 LastName2, ...
  31.  
  32. Note: There will be a 10 pt penalty if your names do not follow
  33. this format.
  34.  
  35. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  36. For this homework you will need to use ACL2s.
  37.  
  38. Technical instructions:
  39.  
  40. - open this file in ACL2s as hw07.lisp
  41.  
  42. - make sure you are in BEGINNER mode. This is essential! Note that you can
  43. only change the mode when the session is not running, so set the correct
  44. mode before starting the session.
  45.  
  46. - insert your solutions into this file where indicated (usually as "...")
  47.  
  48. - only add to the file. Do not remove or comment out anything pre-existing
  49. unless asked to.
  50.  
  51. - make sure the entire file is accepted by ACL2s. In particular, there must
  52. be no "..." left in the code. If you don't finish all problems, comment
  53. the unfinished ones out. Comments should also be used for any English
  54. text that you may add. This file already contains many comments, so you
  55. can see what the syntax is.
  56.  
  57. - when done, save your file and submit it as hw07.lisp
  58.  
  59. - avoid submitting the session file (which shows your interaction with the
  60. theorem prover). This is not part of your solution. Only submit the lisp
  61. file!
  62.  
  63. Instructions for programming problems:
  64.  
  65. For each function definition, you must provide both contracts and a body.
  66.  
  67. You must also ALWAYS supply your own tests. This is in addition to the
  68. tests sometimes provided. Make sure you produce sufficiently many new test
  69. cases. This means: cover at least the possible scenarios according to the
  70. data definitions of the involved types. For example, a function taking two
  71. lists should have at least 4 tests: all combinations of each list being
  72. empty and non-empty.
  73.  
  74. Beyond that, the number of tests should reflect the difficulty of the
  75. function. For very simple ones, the above coverage of the data definition
  76. cases may be sufficient. For complex functions with numerical output, you
  77. want to test whether it produces the correct output on a reasonable
  78. number of inputs.
  79.  
  80. Use good judgment. For unreasonably few test cases we will deduct points.
  81. The markers have been given permission to add any tests they want. Thus one
  82. way to tell how many tests you need: are you positive the markers won't break
  83. your code?
  84.  
  85. We will use ACL2s' check= function for tests. This is a two-argument
  86. function that rejects two inputs that do not evaluate equal. You can think
  87. of check= roughly as defined like this:
  88.  
  89. (defunc check= (x y)
  90. :input-contract (equal x y)
  91. :output-contract (equal (check= x y) t)
  92. t)
  93.  
  94. That is, check= only accepts two inputs with equal value. For such inputs, t
  95. (or "pass") is returned. For other inputs, you get an error. If any check=
  96. test in your file does not pass, your file will be rejected.
  97.  
  98. You should also use test? for your tests. See Section 2.13 of the
  99. lecture notes. An example of test? is the following.
  100.  
  101. (test? (implies (and (listp l) (natp n))
  102. (<= (len (sublist-start n)) n)))
  103.  
  104. |#
  105.  
  106. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  107. ;PART I. PROGRAMMING AND CONTRACTS
  108. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  109. ; Lists of lists
  110.  
  111. ; A list can itself contain lists, and these lists can contain other lists
  112. ; to any number of levels. The listp function only checks whether one has
  113. ; a list at the top level. It does not check any of its elements. Develop
  114. ; a recognizer that checks whether every element of a list l or any list
  115. ; contained in l (to any number of levels) contains only atoms or lists. A list of
  116. ; lists can not have a non-list cons at any level.
  117.  
  118. ; 1. Define
  119. ; pure-listp: All -> Boolean
  120. ; (pure-listp l) is t iff l is a list of atoms or lists, each list that
  121. ; it contains is also a list of atoms or lists, and so on to any number
  122. ; of levels.
  123.  
  124. (defunc pure-listp (l)
  125. :input-contract t
  126. :output-contract (booleanp (pure-listp l))
  127. (if (listp l)
  128. (if (endp l)
  129. t
  130. (if (listp (first l))
  131. (and (pure-listp (first l)) (pure-listp (rest l)))
  132. (and (atom (first l)) (pure-listp (rest l)))))
  133. nil))
  134.  
  135. (check= (pure-listp nil) t)
  136. (check= (pure-listp (cons 1 2)) nil)
  137. (check= (pure-listp 4) nil)
  138. (check= (pure-listp (list 1 2 (list 3 (cons 4 3)))) nil)
  139. ;; Add more tests
  140.  
  141. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  142.  
  143. ; Count the number of times that e occurs in a pure list
  144. ; to any number of levels.
  145.  
  146. ; 2. Define
  147. ; count: All x Purelist -> Nat
  148. ; (count e l) is the number of times that e occurs in a pure list
  149. ; either in the list itself, or in an list in the list, to any
  150. ; number of levels.
  151. (defunc count (e l)
  152. :input-contract (pure-listp l)
  153. :output-contract (natp (count e l))
  154. (if (endp l)
  155. 0
  156. (if (listp (first l))
  157. (count e (first l))
  158. (if (equal e (first l))
  159. (+ 1 (count e (rest l)))
  160. (count e (rest l))))))
  161.  
  162. (check= (count 2 (list 2 (list 1 2 (list 3 2)))) 3)
  163. (check= (count 2 (list 2 (list (list 3 2) 1) 3)) 2)
  164. ;; Add more tests
  165.  
  166. :logic
  167.  
  168. ; odd-even ratio
  169. ; define a list of naturals
  170. (defdata natlist (listof Nat))
  171.  
  172. ; 3.
  173. ; Define a function odd-even-ratio that takes in a natlist
  174. ; and returns the ratio of the sum of odd numbers in the list
  175. ; to the sum of even numbers in the list. If the sum of even
  176. ; numbers is 0, return 1
  177.  
  178.  
  179. :program ; DO NOT comment this!
  180. ; HINT: you may use helper function(s) and/or define your own
  181. ; datatype to make the code modular and readable
  182. (defunc sum-even (l i)
  183. :input-contract (and (natlistp l) (rationalp i))
  184. :output-contract (rationalp i)
  185. (if (endp l)
  186. i
  187. (if (integerp (/ (first l) 2))
  188. (sum-even (rest l) (+ i (first l)))
  189. (sum-even (rest l) i))))
  190.  
  191. (defunc sum-odd (l i)
  192. :input-contract (and (natlistp l) (rationalp i))
  193. :output-contract (rationalp i)
  194. (if (endp l)
  195. i
  196. (if (not (integerp (/ (first l) 2)))
  197. (sum-odd (rest l) (+ i (first l)))
  198. (sum-odd (rest l) i))))
  199.  
  200. (check= (sum-even '(1 2 3 4 5 6) 0) 12)
  201.  
  202.  
  203. ; odd-even-ratio: Natlist -> Rational
  204. ; takes in a natlist and returns the ratio of the sum of odd
  205. ; numbers in the list to the sum of even numbers in the list.
  206. ; If the sum of even numbers is 0, return 1
  207. (defunc odd-even-ratio (l)
  208. :input-contract (natlistp l)
  209. :output-contract (rationalp (odd-even-ratio l))
  210. (if (equal (sum-even l 0) 0)
  211. 1
  212. (/ (sum-odd l 0) (sum-even l 0))))
  213.  
  214. (check= (odd-even-ratio '(1 2 3 4 5 6)) (/ 9 12))
  215. (check= (odd-even-ratio '(3 2 5 4 6 1)) (/ 3 4))
  216. ;; Add more tests
  217.  
  218. :logic
  219. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  220. ; CONTRACTS
  221. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  222. ;Identify if there is a body contract or a function contract violation. If so,
  223. ;provide an input for which the violation occurs and point to the exact
  224. ;location of the error.
  225.  
  226. #|
  227. 4. (defunc f (x)
  228. :input-contract (posp x)
  229. :output-contract (integerp (f x))
  230. (if (equal x 1)
  231. 9
  232. (- 10 (f (- x 2)))))
  233. |#
  234. ;(f 2)
  235. ;(f (- 2 2)) -> (f 0) -> (posp 0) = false
  236. ;body-contract violation
  237. #|
  238. 5. (defunc f (x y)
  239. :input-contract (and (listp x) (posp y))
  240. :output-contract (listp (f x y))
  241. (if (equal y 1)
  242. nil
  243. (f (list (first x)) (- y 1))))
  244. |#
  245. ;(f nil 2)
  246. ;(first nil) -> cant call first of nil
  247. #|
  248. 6. (defunc f (x y)
  249. :input-contract (and (listp x) (listp y))
  250. :output-contract (posp (f x y))
  251. (if (endp x)
  252. 0
  253. (+ 1 (f (rest x) y))))
  254. |#
  255. ;(f nil nil)
  256. ;f returns 0 -> (posp 0) = false
  257. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  258. ;PART II. PROPOSITIONAL LOGIC
  259. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  260. ;A. Simplify using rules of propositional logic - DO NOT give a truth table as the solution
  261. ;; Your solutions should have the fewest number of propositional operators that you can give
  262. ;; thus ~(A \/ ~B) should be simplified to ~A /\ B. Parentheses do not count as operators.
  263.  
  264. ;1. ~(p=>q) \/ ~(q=>p)
  265. ;(p && !q) || (q && !p)
  266. ;
  267.  
  268. ;2. q => (((p \/ q) /\ p) /\ ((p /\ q) \/ q) )
  269. ;......................
  270.  
  271.  
  272. ;3. p => (~q \/ r \/ ((q => r) /\ p))
  273. ;......................
  274.  
  275.  
  276. ;4. ~(r => ~q) \/ ~(p => ~r)
  277. ;......................
  278.  
  279. ;5. ~((p => q) /\ ~r)
  280. ;......................
  281.  
  282. ;6. p => p => p
  283. ;......................
  284.  
  285. ;7. ((p <=> q) <=> (p <> q)) => ((p \/ q) => (q=>p))
  286. ;......................
  287.  
  288.  
  289. ;;----------------------------------------------
  290. ;; B.Checking if a formula is unsatisfiable
  291. ;; Suppose we want to build a procedure IS_UNSAT(f) that can check if the input
  292. ;; propositional logic formula f is unstaisfiable or not. However, suppose we only have
  293. ;; access to a procedure IS_VALID(f) that can check if the input propositional
  294. ;; formula f is valid. How do we build IS_UNSAT(F)? An explanation in English is fine.
  295. ;; Hint: we can use propositional logic connectives ...
  296. ;......................
  297.  
  298.  
  299. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  300. ;PART III. EQUATIONAL REASONING
  301. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  302. #|
  303. (defunc foo (m n)
  304. :input-contract (and (natp m) (natp n))
  305. :output-contract (natp (foo m n))
  306. (cond ((equal m 0) (+ n 1))
  307. ((equal n 0) (foo (- m 1) 1))
  308. (t (foo (- m 1) (foo m (- n 1))))))
  309.  
  310. 1. Prove the following using equational reasoning:
  311.  
  312. (not (or (not (implies (natp n)
  313. (equal (foo 0 n) (+ 1 n))))
  314. (not (implies (and (natp n)
  315. (equal n 0))
  316.  
  317. (equal (foo 1 n) (+ 2 n))))))
  318. |#
  319. ;......................
  320.  
  321. #|
  322. 2. Prove the following using equational reasoning:
  323.  
  324. (implies (and (natp n)
  325. (not (equal n 0))
  326. (implies (natp (- n 1))
  327. (equal (foo 1 (- n 1)) (+ 2 (- n 1)))))
  328. (equal (foo 1 n) (+ 2 n)))
  329.  
  330. ;......................
  331. |#
  332.  
  333. #|
  334. 3. Fill in the ...'s below so that the resulting formula is a
  335. theorem.
  336.  
  337. The first ... is the what you get from contract completion. See
  338. the lecture notes if you need a reminder of what contract
  339. completion is. Contract completion should not include any
  340. unnecessary terms.
  341.  
  342. For the second ..., you cannot use the function "a" in your
  343. answer, e.g., you cannot simply replace ... by "(a 2 n)". The
  344. ... has to be replace by an arithmetic expression, i.e., an
  345. expression involving arithmetic functions we have already
  346. seen(such as +, -, *, and /).
  347.  
  348. ***Note that you do not have to prove anything.***
  349. |#
  350. #|
  351. (implies (natp n)
  352. (equal (foo 2 n) ...))
  353. ;......................
  354. |#
  355.  
  356. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  357. ; The following equational reasoning problem (Question 4) is for extra practice, and
  358. ; will not be graded
  359. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  360. #|
  361. 4. Consider the following definitions:
  362.  
  363. (defunc len (l)
  364. :input-contract (listp l)
  365. :output-contract (natp (len l))
  366. (if (endp l)
  367. 0
  368. (+ 1 (len (rest l)))))
  369.  
  370. (defunc ziplists (x y)
  371. :input-contract (and (listp x) (listp y) (equal (len x) (len y)))
  372. :output-contract (listp (ziplists x y))
  373. (if (endp x)
  374. nil
  375. (cons (list (first x) (first y))
  376. (ziplists (rest x) (rest y)))))
  377.  
  378. Prove the following using equational reasoning:
  379. phi:
  380. (implies (and (listp x) (listp y) (equal (len x) (len y)))
  381. (and (implies (endp x)
  382. (equal (len (ziplists x y))
  383. (len x)))
  384. (implies (and (consp x)
  385. (equal (len (ziplists (rest x) (rest y)))
  386. (len (rest x))))
  387. (equal (len (ziplists x y))
  388. (len x)))))
  389. |#
  390. #|
  391. ;......................
  392. |#
  393.  
  394. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  395. ;PART IV. ADMISSIBILITY
  396. ; THIS WILL NOT BE ON EXAM 1:
  397. ; so you may wait until exam1
  398. ; is over to attempt this!
  399. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  400. #|
  401. This problem set is about the "Definitional Principle".
  402.  
  403. For each of the definitions below, check whether it is admissible, i.e. it
  404. satisfies all rules of the definitional principle. You can assume that Rule
  405. 1 is met: the symbol used in the defunc is a new function symbol in each
  406. case.
  407.  
  408. If you claim admissibility,
  409.  
  410. 1. Explain in English why the body contracts hold.
  411. 2. Explain in English why the contract theorem holds.
  412. 3. Suggest a measure function that can be used to show termination.
  413. (You DO NOT have to prove the measure function properties in this problem.)
  414.  
  415. Otherwise, identify a rule in the Definitional Principle that is violated.
  416.  
  417. If you blame one of the purely syntactic rules (variable names,
  418. non-wellformed body etc), explain the violation in English.
  419.  
  420. If you blame one of the semantic rules (body contract, contract theorem or
  421. termination), you must provide an input that satisfies the input contract, but
  422. causes a violation in the body or violates the output contract or causes
  423. non-termination.
  424.  
  425. Remember that the rules are not independent: if you claim the function does
  426. not terminate, you must provide an input on which the function runs forever
  427. *without* causing a body contract violation: a body contract violation is
  428. not a counterexample to termination. Similarly, if you claim the function
  429. fails the contract theorem, you must provide an input on which it
  430. terminates and produces a value, which then violates the output contract.
  431. Assume that each function symbol f is a new one (i.e., not already admitted
  432. by ACL2s before).
  433.  
  434.  
  435. 1. (defunc f (x)
  436. :input-contract (posp x)
  437. :output-contract (posp (f x))
  438. (if (equal x 1)
  439. 9
  440. (f (+ 10 (f (- x 1))))))
  441. ;......................
  442.  
  443. 2. (defunc f (x)
  444. :input-contract (integerp x)
  445. :output-contract (natp (f x))
  446. (if (>= x 0)
  447. (- (* x x) x)
  448. (+ x (* x x))))
  449. ;......................
  450.  
  451. 3. (defunc f (x l)
  452. :input-contract (and (integerp x) (listp l))
  453. :output-contract (natp (f x l))
  454. (if (equal x 0)
  455. 0
  456. (if (> x 0)
  457. (f (- x 1) (cons x l))
  458. (f (+ x 1) (cons x l)))))
  459. ;......................
  460.  
  461. 4. (defunc f (x)
  462. :input-contract (integerp x)
  463. :output-contract (integerp (f x))
  464. (if (equal x 1)
  465. 9
  466. (- 10 (f (- x 1)))))
  467. ;......................
  468. |#
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement