Class: Bud::Lattice

Inherits:
Object
  • Object
show all
Includes:
Comparable
Defined in:
lib/bud/lattice-core.rb

Constant Summary

@@lattice_kinds =
{}
@@global_morphs =
Set.new
@@global_mfuncs =
Set.new

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.global_mfuncsObject



61
62
63
# File 'lib/bud/lattice-core.rb', line 61

def self.global_mfuncs
  @@global_mfuncs
end

.global_morphsObject



43
44
45
# File 'lib/bud/lattice-core.rb', line 43

def self.global_morphs
  @@global_morphs
end

.lattice_kindsObject



21
22
23
# File 'lib/bud/lattice-core.rb', line 21

def self.lattice_kinds
  @@lattice_kinds
end

.mfuncsObject



57
58
59
# File 'lib/bud/lattice-core.rb', line 57

def self.mfuncs
  @mfuncs || Set.new
end

.monotone(name, &block) ⇒ Object



47
48
49
50
51
52
53
54
55
# File 'lib/bud/lattice-core.rb', line 47

def self.monotone(name, &block)
  if morphs.include?(name) || @@global_morphs.include?(name)
    raise Bud::CompileError, "#{name} declared as both monotone and morph"
  end
  @mfuncs ||= Set.new
  @mfuncs << name
  @@global_mfuncs << name
  define_method(name, &block)
end

.morph(name, &block) ⇒ Object



29
30
31
32
33
34
35
36
37
# File 'lib/bud/lattice-core.rb', line 29

def self.morph(name, &block)
  if mfuncs.include?(name) || @@global_mfuncs.include?(name)
    raise Bud::CompileError, "#{name} declared as both monotone and morph"
  end
  @morphs ||= Set.new
  @morphs << name
  @@global_morphs << name
  define_method(name, &block)
end

.morphsObject



39
40
41
# File 'lib/bud/lattice-core.rb', line 39

def self.morphs
  @morphs || Set.new
end

.wrapperObject



25
26
27
# File 'lib/bud/lattice-core.rb', line 25

def self.wrapper
  @wrapper_name
end

.wrapper_name(name) ⇒ Object



10
11
12
13
14
15
16
17
18
19
# File 'lib/bud/lattice-core.rb', line 10

def self.wrapper_name(name)
  if @wrapper_name
    raise Bud::CompileError, "lattice #{self.name} has multiple wrapper names"
  end
  if @@lattice_kinds.has_key? name
    raise Bud::CompileError, "duplicate lattice definition: #{name}"
  end
  @@lattice_kinds[name] = self
  @wrapper_name = name
end

Instance Method Details

#<=>(o) ⇒ Object

Similarly, use reveal'ed value to implement Comparable.



90
91
92
# File 'lib/bud/lattice-core.rb', line 90

def <=>(o)
  reveal <=> o.reveal
end

#==(o) ⇒ Object

The default equality semantics for lattice objects is based on reveal. Note that this isn't always appropriate: if the intended equality semantics for the lattice type differ from the equality semantics of the object returned by reveal (e.g., a set lattice might return an array with an unpredictable order), the lattice type should override this behavior.



75
76
77
78
# File 'lib/bud/lattice-core.rb', line 75

def ==(o)
  return false unless o.kind_of? Bud::Lattice
  return reveal == o.reveal
end

#eql?(o) ⇒ Boolean

Returns:

  • (Boolean)


80
81
82
# File 'lib/bud/lattice-core.rb', line 80

def eql?(o)
  self == o
end

#hashObject

Ensure hashing and equality semantics are consistent.



85
86
87
# File 'lib/bud/lattice-core.rb', line 85

def hash
  reveal.hash
end

#inspectObject



101
102
103
# File 'lib/bud/lattice-core.rb', line 101

def inspect
  "<#{self.class.wrapper}: #{reveal.inspect}>"
end

#reject_input(i, meth = "initialize") ⇒ Object

Raises:



65
66
67
68
# File 'lib/bud/lattice-core.rb', line 65

def reject_input(i, meth="initialize")
  site = "#{self.class.wrapper}\##{meth}"
  raise Bud::TypeError, "illegal input to #{site}: #{i.inspect}"
end

#revealObject

Return the state valued associated with a lattice instance. Note that this is non-monotonic when invoked from user code; it should be used with care by framework code.



97
98
99
# File 'lib/bud/lattice-core.rb', line 97

def reveal
  @v
end

#wrap_unsafe(new_v) ⇒ Object

Construct a new instance of the current class that wraps “new_v”. We assume that new_v is already a legal input value for the class, so we can bypass the class's normal initializer – this avoids redundant error checks.



108
109
110
111
112
# File 'lib/bud/lattice-core.rb', line 108

def wrap_unsafe(new_v)
  rv = self.class.new
  rv.instance_variable_set('@v', new_v)
  rv
end