Created
January 26, 2022 14:41
-
-
Save bon/59c0feb3546a9d3de1d466ae201bf8db to your computer and use it in GitHub Desktop.
Coalton tests using fiasco
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(ql:quickload '(:coalton :fiasco)) | |
(defpackage :coalton-derivative | |
(:use :coalton :coalton-library)) | |
(in-package :coalton-derivative) | |
(coalton-toplevel | |
(define-type Symbol | |
(Symbol String)) | |
(define (symbol-name sym) | |
(match sym | |
((Symbol s) s))) | |
(define-instance (Eq Symbol) | |
(define (== a b) | |
(== (symbol-name a) (symbol-name b)))) | |
(define-type Expr | |
"A symbolic expression of basic arithmetic." | |
(EConst Integer) | |
(EVar Symbol) | |
(E+ Expr Expr) | |
(E* Expr Expr)) | |
(declare diff (Symbol -> Expr -> Expr)) | |
(define (diff x f) | |
"Compute the derivative of F with respect to X." | |
(match f | |
((EConst _) ; c' = 0 | |
(EConst 0)) | |
((EVar s) ; x' = 1 | |
(if (== s x) (EConst 1) (EConst 0))) | |
((E+ a b) ; (a+b)' = a' + b' | |
(E+ (diff x a) (diff x b))) | |
((E* a b) ; (ab)' = a'b + ab' | |
(E+ (E* (diff x a) b) | |
(E* a (diff x b)))))) | |
(declare dt (Expr -> Expr)) | |
(define dt | |
"The time derivative operator." | |
(diff (Symbol "t")))) | |
(fiasco:define-test-package :coalton-derivative-test | |
(:use :coalton-derivative) | |
(:shadowing-import-from :coalton-derivative dt E+ EVar Symbol EConst E+) | |
(:import-from :coalton coalton) | |
(:import-from :coalton-library ==)) | |
(cl:in-package :coalton-derivative-test) | |
(deftest diff-test () | |
(is (coalton (== (dt (E+ (EVar (Symbol "t")) | |
(EConst 1))) | |
(E+ (EConst 1) (EConst 10)))))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment