Module: Dhallish::Types

Defined in:
lib/types.rb

Defined Under Namespace

Classes: Function, List, Optional, Record, Type, Union, Unresolved

Constant Summary collapse

Natural =
:Natural
Integer =
:Integer
Double =
:Double
Bool =
:Bool
Text =
:Text
Numbers =
[Natural, Integer, Double]
TextList =
List.new(Text)
NaturalList =
List.new(Natural)
IntegerList =
List.new(Integer)
DoubleList =
List.new(Double)

Class Method Summary collapse

Class Method Details

.is_a_type?(x) ⇒ Boolean

Returns:

  • (Boolean)


280
281
282
# File 'lib/types.rb', line 280

def is_a_type?(x)
  !not_a_type?(x)
end

.not_a_type?(x) ⇒ Boolean

Returns:

  • (Boolean)


271
272
273
274
275
276
277
# File 'lib/types.rb', line 271

def not_a_type?(x)
  if x.is_a? Symbol
    x != Natural and x != Integer and x != Double and x != Bool and x != Text
  else
    !x.is_a? Optional and !x.is_a? List and !x.is_a? Record and !x.is_a? Function and !x.is_a? Unresolved and !x.is_a? Type and !x.is_a? Union
  end
end

.resolve(orgtype, name, newtype) ⇒ Object



152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
# File 'lib/types.rb', line 152

def resolve(orgtype, name, newtype)
  case orgtype
  when Type
    Type.new(resolve(orgtype., name, newtype))
  when Unresolved
    if name == orgtype.name
      newtype
    else
      orgtype
    end
  when Function
    if orgtype.restype.nil?
      Function.new(resolve(orgtype.argtype, name, newtype), nil, orgtype.unres)
    else
      restype = orgtype.restype
      argtype = orgtype.argtype
      if orgtype.unres.nil? or orgtype.unres != name
        restype = resolve(orgtype.restype, name, newtype)
        argtype = resolve(orgtype.argtype, name, newtype)
      end
      Function.new(argtype, restype, orgtype.unres)
    end
  when Optional
    Optional.new(resolve(orgtype.type, name, newtype))
  when List
    List.new(resolve(orgtype.type, name, newtype))
  when Record
    Record.new(orgtype.types.map{ |key, val| [key, resolve(val, name, newtype)] }.to_h)
  when Union
    Union.new(orgtype.types.map{ |key, val| [key, resolve(val, name, newtype)] }.to_h)
  else
    orgtype
  end
end

.unification(a, b, mapping = {}) ⇒ Object



188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
# File 'lib/types.rb', line 188

def unification(a, b, mapping={})
  case a
  when Type
    if b.is_a? Type
      unification(a., b., mapping)
    else
      nil
    end
  when Unresolved
    if b.is_a? Unresolved
      if mapping[a.name] == nil
        mapping[a.name] = b.name
        mapping
      elsif mapping[a.name] != b.name
        nil
      else
        mapping
      end
    else
      nil
    end
  when Function
    if b.is_a? Function
      if unification(a.argtype, b.argtype, mapping).nil?
        nil
      else
        arestype = a.restype
        brestype = b.restype
        if !a.unres.nil?; arestype = resolve(a.restype, a.unres, Unresolved.new(get_new_sym())) end
        if !b.unres.nil?; brestype = resolve(b.restype, b.unres, Unresolved.new(get_new_sym())) end
        unification(arestype, brestype, mapping)
      end
    else
      nil
    end
  when Optional
    if b.is_a? Optional
      unification(a.type, b.type, mapping)
    else
      nil
    end
  when List
    if b.is_a? List
      unification(a.type, b.type, mapping)
    else
      nil
    end
  when Record
    if b.is_a? Record and a.types.keys.length == b.types.keys.length
      for key in a.types.keys
        aelm = a.types[key]
        belm = b.types[key]
        if belm.nil?; return nil end
        ret = unification(aelm, belm, mapping)
        if ret.nil?; return nil end
      end
      mapping
    else
      nil
    end
  when Union
    if b.is_a? Union and a.types.keys.length == b.types.keys.length
      for key in a.types.keys
        aelm = a.types[key]
        belm = b.types[key]
        if belm.nil?; return nil end
        ret = unification(aelm, belm, mapping)
        if ret.nil?; return nil end
      end
      mapping
    else
      nil
    end
  else
    if a == b
      mapping
    else
      nil
    end
  end
end