
; letterfall/kont.sscm - implementation of CPS transform for use with event-driven glib
; Copyright (C) 2012 Alan Manuel K. Gloria
;
; This file is part of Letterfall.
;
; This program is free software; you can redistribute it and/or modify
; it under the terms of the GNU General Public License as published by
; the Free Software Foundation, either version 3 of the License, or
; (at your option) any later version.
;
; This program is distributed in the hope that it will be useful,
; but WITHOUT ANY WARRANTY, without even the implied warranty of
; MERCHANTIBILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
; GNU General Public License for more details.
;
; You should have received a copy of the GNU General Public License
; along with this program.  If not, see <http://www.gnu.org/licenses/>

define-module
  letterfall kont
  :export-syntax
  \\
    kdo
    lift
  :export
  \\
    ; needed by kdo and lift macros
    kont:kdo-bind
    kont:lift
    ; kmonads
    kmonad-wrap
    kmonad?
    ; execute a kmonad
    execute-kmonad

use-modules
  ice-9 syncase
  srfi srfi-9

define-record-type kmonad
  kmonad-wrap f
  kmonad?
  f kmonad-unwrap

define-syntax kdo
  syntax-rules (let)
    \\
    ! kdo
    !   x
    ! \\
    ! x
    \\
    ! kdo
    !   let v \\ x
    !   body
    !   ...
    ! \\
    ! kont:kdo-bind x
    ! ! lambda (v)
    ! !   kdo
    ! !     body
    ! !     ...
    \\
    ! kdo
    !   x
    !   body
    !   ...
    ! \\
    ! kont:kdo-bind x
    ! ! lambda (_)
    ! !   kdo
    ! !     body
    ! !     ...
define kont:kdo-bind(fkm g)
  let
    $ f $ kmonad-unwrap fkm
    kmonad-wrap $ lambda (pass)
      f $ lambda (a)
            kmonad-unwrap(g(a)) pass

define-syntax lift
  syntax-rules ()
    \\
    ! lift x
    ! \\
    ! kont:lift $ lambda () x
define kont:lift(f)
  kmonad-wrap $ lambda (pass)
    pass f()

define execute-kmonad(kmonad pass)
  kmonad-unwrap(kmonad) pass

; vim: set ft=scheme : ;
