Finished Forth example
Getty Ritter
11 years ago
| 409 | 409 | get : Forth (a, b) -> a |
| 410 | 410 | drop : Forth (a, b) -> Forth b |
| 411 | 411 | swap : Forth (a, (b, c)) -> Forth (b, (a, c)) |
| 412 |
a |
|
| 412 | app : Forth (a, (b, c)) -> (a -> b -> d) -> Forth (d, c) | |
| 413 | 413 | \end{verbatim} |
| 414 | 414 | } |
| 415 | 415 | |
| 450 | 450 | \end{verbatim} |
| 451 | 451 | } |
| 452 | 452 | |
| 453 | We have to create a helper function in order to create a value of this | |
| 454 | type, which is the helper function \dualtt{push'}: | |
| 455 | ||
| 456 | {\color{NavyBlue} | |
| 457 | \begin{verbatim} | |
| 458 | doPush : Forth a -> b -> Forth (b, a) | |
| 459 | doPush s x = merge s' from | |
| 460 | push s' <- doPush s' | |
| 461 | get s' <- x | |
| 462 | drop s' <- s | |
| 463 | swap s' <- push (get s) (push x (drop s)) | |
| 464 | app s' <- | |
| 465 | let doApp f = push (f x (get s)) (drop s) | |
| 466 | in doApp | |
| 467 | \end{verbatim} | |
| 468 | } | |
| 469 | ||
| 470 | And then we can create an actual value of type \dualtt{Forth ()}: | |
| 471 | ||
| 472 | {\color{NavyBlue} | |
| 473 | \begin{verbatim} | |
| 474 | initialState : Forth () | |
| 475 | initialState = merge s' | |
| 476 | push s' <- doPush s' | |
| 477 | \end{verbatim} | |
| 478 | } | |
| 479 | ||
| 453 | 480 | \end{document} |