Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > [ANN] MLTypes: ML-style qualified unions for ruby

Reply
Thread Tools

[ANN] MLTypes: ML-style qualified unions for ruby

 
 
Logan Capaldo
Guest
Posts: n/a
 
      09-19-2005
I've always liked ML-style languages for declaring new types, and I
thought hey, let me try to do that in ruby, its already got a case
statement that reminds me of ML. So on with the examples:

% cat example.rb
require 'mltypes'

deftype :List do
:Cons.of( :first => :Object, :rest => :List) | :End
end

deftype :Tree do
:Leaf.of(:data => :Object) | :Branch.of( :data
=> :Object, :left => :Tree, :right => :Tree )
end

def print_list(list)
case list.tag
when :Cons
puts list.first
print_list(list.rest)
when :End
end
end


def print_tree_inorder(tree)
case tree.tag
when :Leaf
puts tree.data
when :Branch
print_tree_inorder(tree.left)
puts tree.data
print_tree_inorder(tree.right)
end
end

ls = List.Cons(:first => 1, :rest => List.End)
ls = List.Cons( :first => "Hello", :rest => ls )
print_list ls

tree = Tree.Branch( :data => 2, :left => Tree.Leaf( :data =>
1 ), :right => Tree.Leaf( :data => 3 ))

print_tree_inorder(tree)

__END__

Not bad huh?

The other interesting thing to note (for me anyway is)

deftype :MyBoolean do
:Yes | :No # bar works here
end

:maybe | :so # not here though
NoMethodError: undefined method `|' for :maybe:Symbol
from (irb):6

So it shouldn't mess up any existing stuff that relies on Symbol not
having |.
OTOH, its kind of heavy -handed (ie if you defined | for something
else it will mess stuff up completely. Just keep that in mind)

Now here is the code that does all this fun.

% cat mltypes.rb
module MLTypes
class Alternation
def initialize(alts = [])
@alts = alts
end
def |(other)
@alts << other
self
end

def alternatives
@alts
end
end
class TaggedTuple
def initialize(tag, pairs)
@tag = tag
@pairs = pairs
end

def |(other)
Alternation.new([self, other])
end

def tag
@tag
end

def pairs
@pairs
end
end
end
def deftype(name)
res = nil
begin
Symbol.class_eval {
define_method(:"|") do |other|
MLTypes::Alternation.new([self, other])
end

define_method(f) do |pairs|
MLTypes::TaggedTuple.new(self, pairs)
end
}
res = yield
ensure
Symbol.class_eval {
remove_method(:"|") rescue nil
remove_method(f) rescue nil
}

end

Object.const_set(name.to_s, Class.new)
klass = Object.const_get name

klass.class_eval {
define_method(:tag) do
@tag
end
m = instance_method(:method_missing)

define_method(:method_missing) do |*args|
if not @state.nil? and @state.has_key?(args[0])
@state[args[0]]
else
m.bind(self).call(*args)
end
end

res.alternatives.each do |enum|
obj = klass.new
metaklass = (class << self; self; end)
if enum.kind_of?(MLTypes::TaggedTuple)
metaklass.class_eval {
define_method(enum.tag) { |
args|
state = {}
enum.pairs.keys.each
do |key|
raise
TypeError, "Expected #{enum.pairs[key]} got #{args[key].class}"
unless args[key].kind_of? Object.const_get(enum.pairs[key])
state[key] =
args[key]
end
res =
klass.class_eval { new }
res.instance_eval
{ @state = state; @tag = enum.tag }
res
}
}
else # regular enum
obj.instance_eval { @tag = enum }
metaklass.class_eval {
define_method(enum) do
obj
end
}
end
end

class << self
private :new
end
}

end

__END__







 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Pointer to qualified poitner to qualified object Szabolcs Borsanyi C Programming 13 06-08-2008 07:52 PM
Number of Qualified MCSD.NET Individuals lighthammer MCSD 6 02-21-2005 09:38 AM
REPOST FormsAuthentication.RedirectFromLoginPage is not passed fully qualified url Jacob Crossley ASP .Net 0 04-06-2004 03:13 PM
FormsAuthentication.RedirectFromLoginPage is not passed fully qualified url Jacob Crossley ASP .Net 0 04-02-2004 04:04 PM
Re: fully qualified domain name Frank Drebin ASP .Net 0 08-26-2003 08:43 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57