Advertisement
DoromaAnim

Untitled

Apr 10th, 2019
157
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.61 KB | None | 0 0
  1. #lang racket
  2.  
  3. ;; definicja wyrażeń z let-wyrażeniami
  4.  
  5. (struct const (val) #:transparent)
  6. (struct op (symb l r) #:transparent)
  7. (struct let-expr (x e1 e2) #:transparent)
  8. (struct variable (x) #:transparent)
  9. (struct sigma (a b f) #:transparent)
  10. (struct integral (a b f) #:transparent)
  11.  
  12. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  13. (struct iff (warunek parw fal))
  14. ;;(struct cond (warunek praw if))
  15. (struct lambda (lista arugmentow funkcja))
  16. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  17.  
  18. ;;(define (expr? e)
  19. ;; (match e
  20. ;; [(variable s) (symbol? s)]
  21. ;; [(const n) (number? n)]
  22. ;; [(op s l r) (and (member s '(+ * ^))
  23. ;; (expr? l)
  24. ;; (expr? r))]
  25. ;; [(sigma a b f) (and (expr? a)
  26. ;; (expr? b)
  27. ;; (procedure? f)
  28. ;; (= (procedure-arity f) 1))]
  29. ;; [(integral a b f) (and (expr? a)
  30. ;; (expr? b)
  31. ;; (procedure? f)
  32. ;; (= (procedure-arity f) 1))]
  33. ;; [(iff a b f) (and (expr? a)
  34. ;; (expr? b)
  35. ;; (procedure? f)
  36. ;; (= (procedure-arity f) 1))]
  37. ;; [(let-expr x e1 e2) (and (symbol? x)
  38. ;; (expr? e1)
  39. ;; (expr? e2))]
  40. ;; [_ false]))
  41. ;;
  42. ;;;; podstawienie wartości (= wyniku ewaluacji wyrażenia) jako stałej w wyrażeniu
  43.  
  44. ;;(define (subst x v e)
  45. ;; (match e
  46. ;; [(op s l r) (op s (subst x v l)
  47. ;; (subst x v r))]
  48. ;; [(const n) (const n)]
  49. ;; [(variable y) (if (eq? x y)
  50. ;; (const v)
  51. ;; (variable y))]
  52. ;; [(let-expr y e1 e2)
  53. ;; (if (eq? x y)
  54. ;; (let-expr y
  55. ;; (subst x v e1)
  56. ;; e2)
  57. ;;;; (let-expr y
  58. ;;;; (subst x v e1)
  59. ;;;; (subst x v e2)))]))
  60. ;;;;
  61. ;;;;;; (gorliwa) ewaluacja wyrażenia w modelu podstawieniowym
  62. ;;;;
  63. ;;;;(define (eval e)
  64. ;;;; (match e
  65. ;;;; [(const n) n]
  66. ;;;; [(op '+ l r) (+ (eval l) (eval r))]
  67. ;;;; [(op '* l r) (* (eval l) (eval r))]
  68. ;;;; [(let-expr x e1 e2)
  69. ;;;; (eval (subst x (eval e1) e2))]
  70. ;;;; [(variable n) (error n "cannot referencexdre its definition ;)")]))
  71. ;;;;
  72. ;;;;;; przykładowe programy
  73. ;;;;
  74. ;;;;
  75. ;;;;;;(define (from-quote t)
  76. ;;;;;; (define (iter xs xd)
  77. ;;;;;; (if (null? xs)
  78. ;;;;;; xd
  79. ;;;;;; (iter (cdr xs) (append (list (car xs)) xd))))
  80. ;;;;;; (iter t `()))
  81. ;;;;
  82. ;;
  83. ;;(define (from-quote t)
  84. ;; (cond
  85. ;; [(number? t) (list `const t)]
  86. ;; [(and (= 1 (length t)) (symbol? (car t))) (list `const 0)]
  87. ;; [(number? (car t)) (list `const t)]
  88. ;; [(and (symbol? (car t)) (= 2 (length t))) (from-quote (second t))]
  89. ;; [else (list `op (car t) (from-quote (second t)) (from-quote (cons (car t) (cddr t))))]))
  90. ;;
  91. ;;
  92. ;;;;(from-quote `(+ 2 (* 1 2) 4))
  93. ;;
  94. ;;
  95. ;;
  96. ;;
  97. ;;(define f (op '* (op '* (variable) (variable))
  98. ;; (variable)))
  99. ;;
  100. ;;;; pochodna funkcji
  101. ;;
  102. ;;(define (∂ f)
  103. ;; (match f
  104. ;; [(const n) (const 0)]
  105. ;; [(variable) (const 1)]
  106. ;; [(op '+ f g) (op '+ (∂ f) (∂ g))]
  107. ;; [(op '* f g) (op '+ (op '* (∂ f) g)
  108. ;; (op '* f (∂ g)))]))
  109. ;;
  110. ;;;; przykładowe użycie
  111. ;;
  112. ;;(define (evali e)
  113. ;; (match e
  114. ;; [(variable) (variable)]
  115. ;; [(const n) (const n)]
  116. ;; [(op `+ (const x) (const y)) (const (+ x y))]
  117. ;; [(op `+ e1 e1) (op `* (const 2) e1)]
  118. ;; [(op `+ e1 e2) (op `+ (evali e1) (evali e2))]
  119. ;; [(op `+ (const 0) e1) e1]
  120. ;; [(op `+ e1 (const 0)) e1]
  121. ;;
  122. ;; [(op `* e1 (const 0)) (const 0)]
  123. ;; [(op `* e1 (const 1)) (evali e1)]
  124. ;; [(op `* (variable) (const 0)) (const 0)]
  125. ;; [(op `* (variable) (const 0)) (const 0)]
  126. ;; [(op `* (const 0) e1) (const 0)]
  127. ;; [(op `* (const 1) e1) (evali e1)]
  128. ;; [(op `* (const x) (const y)) (const (* x y))]
  129. ;;
  130. ;; [(op `* (variable) e1) (op `* (variable) (evali e1))]
  131. ;; [(op `* e1 (variable)) (op `* (evali e1) (variable))]
  132. ;;
  133. ;; [(op `* e1 e2) (op `* (evali e1) (evali e2))]))
  134. ;;
  135. ;;;(define df (∂ f))
  136. ;;;(define ddf (∂ (∂ f)))
  137. ;;(define dddf (∂ (∂ (∂ f))))
  138. ;;
  139. ;;;dddf
  140. ;;
  141. ;;;(evali df)
  142. ;;
  143. ;;(evali (evali (evali (evali (evali (evali (evali ( evali (evali (evali dddf))))))))))
  144. ;;
  145. ;;
  146. ;;(define (make-stos stack)
  147. ;; (define (push x)
  148. ;; (set! stack (mcons x stack)))
  149. ;; (define (top)
  150. ;; (begin (let ((res (mcar stack)))
  151. ;; (pop)
  152. ;; res)))
  153. ;; (define (pop)
  154. ;; (set! stack (mcdr stack)))
  155. ;; (define (dispatch m)
  156. ;; (cond [(eq? m `push) push]
  157. ;; [(eq? m `top) (top)]
  158. ;; [(eq? m `pop) (pop)]))
  159. ;; dispatch)
  160. ;;
  161. ;;(define stosik (make-stos `()))
  162. ;;
  163. ;;(define (eval-onp xs)
  164. ;; (define (iter xs)
  165. ;; (if (null? xs)
  166. ;; (stosik `top)
  167. ;; (if (member (car xs) `(+ - * /))
  168. ;; (match (car xs)
  169. ;; [`+ (begin ((stosik `push) (+ (stosik `top) (stosik `top)))
  170. ;; (iter (cdr xs)))]
  171. ;; [`* (begin ((stosik `push) (* (stosik `top) (stosik `top)))
  172. ;; (iter (cdr xs)))])
  173. ;; (begin ((stosik `push) (car xs))
  174. ;; (iter (cdr xs))))))
  175. ;; (iter xs))
  176. ;;
  177. ;;
  178. ;;
  179. ;;(define e1 (op '+ (op '+ (const 2) (const 2))
  180. ;; (const 2)))
  181. ;;
  182. ;;(define (to-rpn e)
  183. ;; (match e
  184. ;; [(const n) (list n)]
  185. ;; [(op s l r) (append (to-rpn l)
  186. ;; (to-rpn r)
  187. ;; (list s))]))
  188. ;;
  189. ;;(eval-onp (to-rpn e1))
  190.  
  191. (define (find e u)
  192. (match e
  193. [(const x) #f]
  194. [(variable x) (if (eq? u x) #t #f)]
  195. [(op s x y) (or (find x u) (find y u))]
  196. [(let-expr x e1 e2) (or (find e1 u) (find e2 u))]))
  197.  
  198. (define (usun e)
  199. (match e
  200. [(const x) (const x)]
  201. [(variable x) (variable x)]
  202. [(op s x y) (op s (usun x) (usun y))]
  203. [(let-expr x e1 e2) (if (or (find e1 x) (find e2 x))
  204. (let-expr x (usun e1) (usun e2))
  205. (usun e2))]))
  206.  
  207. (usun (let-expr `x
  208. (op `+ (const 2) (const 2))
  209. (let-expr `y
  210. (op `* (const 3) (variable `x))
  211. (op `+ (const 7) (variable `x)))))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement