unofficial mirror of guile-user@gnu.org 
 help / color / mirror / Atom feed
* Contract programming in Guile
@ 2021-07-17 16:01 Olivier Dion via General Guile related discussions
  2021-07-17 16:29 ` Linus Björnstam
  0 siblings, 1 reply; 4+ messages in thread
From: Olivier Dion via General Guile related discussions @ 2021-07-17 16:01 UTC (permalink / raw)
  To: guile-user

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



^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: Contract programming in Guile
  2021-07-17 16:01 Contract programming in Guile Olivier Dion via General Guile related discussions
@ 2021-07-17 16:29 ` Linus Björnstam
  0 siblings, 0 replies; 4+ messages in thread
From: Linus Björnstam @ 2021-07-17 16:29 UTC (permalink / raw)
  To: Olivier Dion, guile-user

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
> 
> 



^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: Contract programming in Guile
  2021-07-18 16:50 ` Kjetil Matheussen
@ 2021-07-19  6:34   ` Linus Björnstam
  0 siblings, 0 replies; 4+ messages in thread
From: Linus Björnstam @ 2021-07-19  6:34 UTC (permalink / raw)
  To: k.s.matheussen, guile-user

Hah! That copyright statement is a lie, it seems.

The original code is for scheme48 and is almost 20 years old, but I cleaned it up and published it in 2017. My intention back in 2017 was to create first-class contracts and use the module system to allow for checked and unchecked imports, but I got a kid in addition to being naturally quite lazy, so that never happened.

 
  Linus Björnstam

On Sun, 18 Jul 2021, at 18:50, Kjetil Matheussen wrote:
> Linus Björnstam:
> >
> > I wrote this a billion million years ago: https://hg.sr.ht/~bjoli/misc/browse/contract.scm?rev=tip
> >
> 
> You know you're getting old when people look back on 2017 as a
> "billion million years ago". :-)
> 
> 



^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: Contract programming in Guile
       [not found] <mailman.57.1626624015.30702.guile-user@gnu.org>
@ 2021-07-18 16:50 ` Kjetil Matheussen
  2021-07-19  6:34   ` Linus Björnstam
  0 siblings, 1 reply; 4+ messages in thread
From: Kjetil Matheussen @ 2021-07-18 16:50 UTC (permalink / raw)
  To: guile-user

Linus Björnstam:
>
> I wrote this a billion million years ago: https://hg.sr.ht/~bjoli/misc/browse/contract.scm?rev=tip
>

You know you're getting old when people look back on 2017 as a
"billion million years ago". :-)



^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2021-07-19  6:34 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-07-17 16:01 Contract programming in Guile Olivier Dion via General Guile related discussions
2021-07-17 16:29 ` Linus Björnstam
     [not found] <mailman.57.1626624015.30702.guile-user@gnu.org>
2021-07-18 16:50 ` Kjetil Matheussen
2021-07-19  6:34   ` Linus Björnstam

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).