Class: Algebrick::ParametrizedType

Inherits:
Type show all
Includes:
FieldMethodReaders, MatcherDelegations, TypeCheck
Defined in:
lib/algebrick/parametrized_type.rb

Instance Attribute Summary (collapse)

Instance Method Summary (collapse)

Methods included from FieldMethodReaders

#add_all_field_method_readers, #add_field_method_reader, #add_field_method_readers, #field_names, #field_names?

Methods included from MatcherDelegations

#!, #&, #>, #>>, #case, #|, #~

Methods included from TypeCheck

#Child!, #Child?, #Match!, #Match?, #Type!, #Type?, error

Methods inherited from Type

#be_kind_of, #match, #name

Methods included from Reclude

#include, #included, #included_into

Methods included from Matching

#any, #match, match_value, #on

Constructor Details

- (ParametrizedType) initialize(variables)

Returns a new instance of ParametrizedType



25
26
27
28
29
30
31
# File 'lib/algebrick/parametrized_type.rb', line 25

def initialize(variables)
  @variables     = variables.each { |v| Type! v, Symbol }
  @fields        = nil
  @variants      = nil
  @cache         = {}
  @cache_barrier = Monitor.new
end

Instance Attribute Details

- (Object) fields (readonly)

Returns the value of attribute fields



23
24
25
# File 'lib/algebrick/parametrized_type.rb', line 23

def fields
  @fields
end

- (Object) variables (readonly)

Returns the value of attribute variables



23
24
25
# File 'lib/algebrick/parametrized_type.rb', line 23

def variables
  @variables
end

- (Object) variants (readonly)

Returns the value of attribute variants



23
24
25
# File 'lib/algebrick/parametrized_type.rb', line 23

def variants
  @variants
end

Instance Method Details

- (Object) ==(other)



89
90
91
92
# File 'lib/algebrick/parametrized_type.rb', line 89

def ==(other)
  other.kind_of? ParametrizedType and
      self.generic == other.generic
end

- (Object) [](*assigned_types)



49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/algebrick/parametrized_type.rb', line 49

def [](*assigned_types)
  @cache_barrier.synchronize do
    @cache[assigned_types] || begin
      raise ArgumentError unless assigned_types.size == variables.size
      ProductVariant.new(type_name(assigned_types)).tap do |type|
        type.be_kind_of self
        @cache[assigned_types] = type
        type.assigned_types    = assigned_types
        type.set_variants *insert_types(variants, assigned_types) if variants
        type.set_fields insert_types(fields, assigned_types) if fields
      end
    end
  end
end

- (Object) call(*field_matchers)

Raises:

  • (TypeError)


84
85
86
87
# File 'lib/algebrick/parametrized_type.rb', line 84

def call(*field_matchers)
  raise TypeError unless @fields
  Matchers::Product.new self, *field_matchers
end

- (Object) generic



64
65
66
# File 'lib/algebrick/parametrized_type.rb', line 64

def generic
  @generic ||= self[*Array(variables.size) { Object }]
end

- (Object) insert_type(type, assigned_types) (private)



107
108
109
110
111
112
113
114
115
116
# File 'lib/algebrick/parametrized_type.rb', line 107

def insert_type(type, assigned_types)
  case type
  when Symbol
    assigned_types[variables.index type]
  when ParametrizedType
    type[*type.variables.map { |v| assigned_types[variables.index v] }]
  else
    type
  end
end

- (Object) insert_types(types, assigned_types) (private)



96
97
98
99
100
101
102
103
104
105
# File 'lib/algebrick/parametrized_type.rb', line 96

def insert_types(types, assigned_types)
  case types
  when Hash
    types.inject({}) { |h, (k, v)| h.update k => insert_type(v, assigned_types) }
  when Array
    types.map { |v| insert_type v, assigned_types }
  else
    raise ArgumentError
  end
end

- (Object) inspect



72
73
74
# File 'lib/algebrick/parametrized_type.rb', line 72

def inspect
  to_s
end

- (Object) set_fields(fields)



33
34
35
36
37
38
39
40
41
42
43
# File 'lib/algebrick/parametrized_type.rb', line 33

def set_fields(fields)
  @fields      = Type! fields, Hash, Array
  @field_names = case @fields
                 when Hash
                   @fields.keys
                 when Array, nil
                   nil
                 else
                   raise
                 end
end

- (Object) set_variants(*variants)



45
46
47
# File 'lib/algebrick/parametrized_type.rb', line 45

def set_variants(*variants)
  @variants = Type! variants, Array
end

- (Object) to_m



76
77
78
79
80
81
82
# File 'lib/algebrick/parametrized_type.rb', line 76

def to_m
  if @variants
    Matchers::Variant.new self
  else
    Matchers::Product.new self
  end
end

- (Object) to_s



68
69
70
# File 'lib/algebrick/parametrized_type.rb', line 68

def to_s
  "#{name}[#{variables.join(', ')}]"
end

- (Object) type_name(assigned_types) (private)



118
119
120
# File 'lib/algebrick/parametrized_type.rb', line 118

def type_name(assigned_types)
  "#{name}[#{assigned_types.join(', ')}]"
end