Class: SFRP::Poly::Node

Inherits:
Object
  • Object
show all
Defined in:
lib/sfrp/poly/elements.rb

Defined Under Namespace

Classes: NodeRef

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(str, node_refs, ftype_annot, eval_func_str, init_func_str = nil) ⇒ Node

Returns a new instance of Node.



56
57
58
59
60
61
62
63
64
# File 'lib/sfrp/poly/elements.rb', line 56

def initialize(
  str, node_refs, ftype_annot, eval_func_str, init_func_str = nil
)
  @str = str
  @node_refs = node_refs
  @ftype_annot = ftype_annot
  @eval_func_str = eval_func_str
  @init_func_str = init_func_str
end

Instance Attribute Details

#strObject (readonly)

Returns the value of attribute str.



52
53
54
# File 'lib/sfrp/poly/elements.rb', line 52

def str
  @str
end

Instance Method Details

#check_recursion(set, path = []) ⇒ Object



82
83
84
85
86
87
88
89
90
# File 'lib/sfrp/poly/elements.rb', line 82

def check_recursion(set, path = [])
  if path.include?(@str)
    raise RecursiveError.new(path.drop_while { |s| s != @str })
  end
  @node_refs.each do |nr|
    next if nr.last
    set.node(nr.node_str).check_recursion(set, path + [@str])
  end
end

#to_mono(monofier) ⇒ Object



92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/sfrp/poly/elements.rb', line 92

def to_mono(monofier)
  raise UndeterminableTypeError.new(@str, @typing) unless @typing.mono?
  type_str = monofier.use_type(@typing)
  node_str = monofier.use_node(@str)
  eval_func_str = monofier.use_func(@eval_func_str, @ftyping)
  if @init_func_str
    init_func_str = monofier.use_func(@init_func_str, @init_ftyping)
  else
    init_func_str = nil
  end
  M.node(type_str, node_str, eval_func_str, init_func_str) do |n|
    @node_refs.each do |node_ref|
      node_str = monofier.use_node(node_ref.node_str)
      node_ref.last ? n.l(node_str) : n.c(node_str)
    end
  end
end

#typing(set) ⇒ Object



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/sfrp/poly/elements.rb', line 66

def typing(set)
  return @typing if @typing
  @typing = Typing.new
  @ftyping = set.func(@eval_func_str).ftyping(set).instance do |eval_ft|
    @node_refs.zip(eval_ft.params) do |node_ref, typing|
      set.node(node_ref.node_str).typing(set).unify(typing)
    end
    next unless @init_func_str
    @init_ftyping = set.func(@init_func_str).ftyping(set).instance do |ft|
      raise unless ft.params.empty?
      ft.body.unify(eval_ft.body)
    end
  end
  @ftyping.unify(@ftype_annot.to_ftyping).body.unify(@typing)
end