Module: ShiftReset

Defined in:
lib/do_notation/monad.rb

Overview

Modeled after Andrzej Filinski’s article “Representing Monads” at POPL’94, and a Scheme implementation of it. citeseer.ist.psu.edu/filinski94representing.html

Constant Summary collapse

@@metacont =
lambda { |x|
  raise RuntimeError, "You forgot the top-level reset..."
}

Instance Method Summary collapse

Instance Method Details

#reset(&block) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
# File 'lib/do_notation/monad.rb', line 10

def reset(&block)
  mc = @@metacont
  callcc { |k|
    @@metacont = lambda { |v|
      @@metacont = mc
      k.call v
    }
    x = block.call
    @@metacont.call x
  }
end

#shift(&block) ⇒ Object



22
23
24
25
26
# File 'lib/do_notation/monad.rb', line 22

def shift(&block)
  callcc { |k|
    @@metacont.call block.call(lambda { |*v| reset { k.call *v } })
  }
end