[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
## Re: Contract programming in Guile

**From**: |
Linus Björnstam |

**Subject**: |
Re: Contract programming in Guile |

**Date**: |
Sat, 17 Jul 2021 18:29:07 +0200 |

**User-agent**: |
Cyrus-JMAP/3.5.0-alpha0-533-gf73e617b8a-fm-20210712.002-gf73e617b |

I wrote this a billion million years ago:
https://hg.sr.ht/~bjoli/misc/browse/contract.scm?rev=tip
Very basic. Contracts are just syntax and not first-class objects.
--
Linus Björnstam
On Sat, 17 Jul 2021, at 18:01, Olivier Dion via General Guile related
discussions wrote:
>* Hi all,*
>* *
>* I'm a big fan of contract programming and was wondering if there's any*
>* SRFI or library that offers this in Guile?*
>* *
>* Here's an example of what I mean by contract:*
>* --------------------------------------------------------------------------------*
>* ;; Given*
>* (define (sum x y z)*
>* *
>* "Return the sum of X, Y and Z."*
>* *
>* (#:pre-conditions*
>* (>= x 0)*
>* (>= y 0)*
>* (>= z 0))*
>* *
>* (#:post-conditions*
>* (>= result 0))*
>* *
>* (+ x y z))*
>* *
>* ;; Would expand to something like*
>* (use-modules (ice-9 local-eval))*
>* *
>* (define (sum x y z)*
>* *
>* "*
>* Return the sum of X, Y, and Z.*
>* *
>* pre-conditions:*
>* (>= x 0)*
>* (>= y 0)*
>* (>= z 0)*
>* *
>* post-conditions:*
>* (>= result 0)*
>* "*
>* *
>* (for-each (lambda (condition)*
>* (unless (local-eval condition (the-environment))*
>* (begin*
>* (format #t "Failed pre-condition: ~a\n" condition)*
>* (exit 1))))*
>* '((>= x 0)*
>* (>= y 0)*
>* (>= z 0)))*
>* *
>* (let ((result (begin (+ x y z))))*
>* (for-each (lambda (condition)*
>* (unless (local-eval condition (the-environment))*
>* (begin*
>* (format #t "Failed post-condition: ~a\n" condition)*
>* (exit 1))))*
>* '((>= result 0)))*
>* result))*
>* --------------------------------------------------------------------------------*
>* *
>* -- *
>* Olivier Dion*
>* PolyMtl*
>* *
>* *