From 8b3356bffca92380d20456255ab35b686e09a982 Mon Sep 17 00:00:00 2001 From: yui-knk Date: Mon, 20 Jan 2025 11:52:52 +0900 Subject: [PATCH 1/5] Migrate digraph.rb rbs to rbs-inline --- lib/lrama/digraph.rb | 18 ++++++++++++++++++ sig/generated/lrama/digraph.rbs | 20 ++++++++++++++++++++ 2 files changed, 38 insertions(+) create mode 100644 sig/generated/lrama/digraph.rbs diff --git a/lib/lrama/digraph.rb b/lib/lrama/digraph.rb index d2bb8810..f8a379f7 100644 --- a/lib/lrama/digraph.rb +++ b/lib/lrama/digraph.rb @@ -1,23 +1,40 @@ +# rbs_inline: enabled # frozen_string_literal: true module Lrama # Algorithm Digraph of https://dl.acm.org/doi/pdf/10.1145/69622.357187 (P. 625) class Digraph + # @rbs sets: String + # @rbs relation: Hash[Integer, Array[Integer]] + # @rbs base_function: Hash[Integer, Integer] + # @rbs return: void def initialize(sets, relation, base_function) # X in the paper + # @rbs @sets: Array[Integer] @sets = sets + # R in the paper + # @rbs @relation: Hash[Integer, Array[Integer]] @relation = relation + # F' in the paper + # @rbs @base_function: Hash[Integer, Integer] @base_function = base_function + # S in the paper + # @rbs @stack: Array[Integer] @stack = [] + # N in the paper + # @rbs @h: Hash[Integer, (Integer|Float)?] @h = Hash.new(0) + # F in the paper + # @rbs @result: Hash[Integer, Integer] @result = {} end + # @rbs () -> Hash[Integer, Integer] def compute @sets.each do |x| next if @h[x] != 0 @@ -29,6 +46,7 @@ def compute private + # @rbs (Integer x) -> void def traverse(x) @stack.push(x) d = @stack.count diff --git a/sig/generated/lrama/digraph.rbs b/sig/generated/lrama/digraph.rbs new file mode 100644 index 00000000..b4d87236 --- /dev/null +++ b/sig/generated/lrama/digraph.rbs @@ -0,0 +1,20 @@ +# Generated from lib/lrama/digraph.rb with RBS::Inline + +module Lrama + # Algorithm Digraph of https://dl.acm.org/doi/pdf/10.1145/69622.357187 (P. 625) + class Digraph + # @rbs sets: String + # @rbs relation: Hash[Integer, Array[Integer]] + # @rbs base_function: Hash[Integer, Integer] + # @rbs return: void + def initialize: (String sets, Hash[Integer, Array[Integer]] relation, Hash[Integer, Integer] base_function) -> void + + # @rbs () -> Hash[Integer, Integer] + def compute: () -> Hash[Integer, Integer] + + private + + # @rbs (Integer x) -> void + def traverse: (Integer x) -> void + end +end From b703b19e11e5efc377256530e7d23599d90496f1 Mon Sep 17 00:00:00 2001 From: yui-knk Date: Mon, 20 Jan 2025 12:06:27 +0900 Subject: [PATCH 2/5] Embedding instance variable type declarations with `@rbs!` --- lib/lrama/digraph.rb | 18 ++++++++++++------ sig/generated/lrama/digraph.rbs | 12 ++++++++++++ sig/lrama/digraph.rbs | 23 ----------------------- 3 files changed, 24 insertions(+), 29 deletions(-) delete mode 100644 sig/lrama/digraph.rbs diff --git a/lib/lrama/digraph.rb b/lib/lrama/digraph.rb index f8a379f7..9fb18df8 100644 --- a/lib/lrama/digraph.rb +++ b/lib/lrama/digraph.rb @@ -4,33 +4,39 @@ module Lrama # Algorithm Digraph of https://dl.acm.org/doi/pdf/10.1145/69622.357187 (P. 625) class Digraph + # TODO: rbs-inline 0.10.0 dosen't support instance variables. + # Move these type declarations above instance variable definitions, once it's supported. + # + # @rbs! + # @sets: Array[Integer] + # @relation: Hash[Integer, Array[Integer]] + # @base_function: Hash[Integer, Integer] + # @stack: Array[Integer] + # @h: Hash[Integer, (Integer|Float)?] + # @result: Hash[Integer, Integer] + # @rbs sets: String # @rbs relation: Hash[Integer, Array[Integer]] # @rbs base_function: Hash[Integer, Integer] # @rbs return: void def initialize(sets, relation, base_function) + # X in the paper - # @rbs @sets: Array[Integer] @sets = sets # R in the paper - # @rbs @relation: Hash[Integer, Array[Integer]] @relation = relation # F' in the paper - # @rbs @base_function: Hash[Integer, Integer] @base_function = base_function # S in the paper - # @rbs @stack: Array[Integer] @stack = [] # N in the paper - # @rbs @h: Hash[Integer, (Integer|Float)?] @h = Hash.new(0) # F in the paper - # @rbs @result: Hash[Integer, Integer] @result = {} end diff --git a/sig/generated/lrama/digraph.rbs b/sig/generated/lrama/digraph.rbs index b4d87236..6a689c77 100644 --- a/sig/generated/lrama/digraph.rbs +++ b/sig/generated/lrama/digraph.rbs @@ -3,6 +3,18 @@ module Lrama # Algorithm Digraph of https://dl.acm.org/doi/pdf/10.1145/69622.357187 (P. 625) class Digraph + @sets: Array[Integer] + + @relation: Hash[Integer, Array[Integer]] + + @base_function: Hash[Integer, Integer] + + @stack: Array[Integer] + + @h: Hash[Integer, (Integer | Float)?] + + @result: Hash[Integer, Integer] + # @rbs sets: String # @rbs relation: Hash[Integer, Array[Integer]] # @rbs base_function: Hash[Integer, Integer] diff --git a/sig/lrama/digraph.rbs b/sig/lrama/digraph.rbs deleted file mode 100644 index 7a61bacc..00000000 --- a/sig/lrama/digraph.rbs +++ /dev/null @@ -1,23 +0,0 @@ -module Lrama - class Digraph - def initialize: (Array[Integer] sets, Hash[Integer, Array[Integer]] relation, Hash[Integer, Integer] base_function) -> void - # X in the paper - @sets: Array[Integer] - # R in the paper - @relation: Hash[Integer, Array[Integer]] - # F' in the paper - @base_function: Hash[Integer, Integer] - # S in the paper - @stack: Array[Integer] - # N in the paper - @h: Hash[Integer, (Integer|Float)?] - # F in the paper - @result: Hash[Integer, Integer] - - def compute: () -> Hash[Integer, Integer] - - private - - def traverse: (Integer x) -> void - end -end From de31392b1ab9fbea7a864ef62158bf254463fa65 Mon Sep 17 00:00:00 2001 From: yui-knk Date: Mon, 20 Jan 2025 12:09:52 +0900 Subject: [PATCH 3/5] Fix type of `sets` parameter It should be aligned with the type of `@sets` --- lib/lrama/digraph.rb | 2 +- sig/generated/lrama/digraph.rbs | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/lrama/digraph.rb b/lib/lrama/digraph.rb index 9fb18df8..a37dc4b6 100644 --- a/lib/lrama/digraph.rb +++ b/lib/lrama/digraph.rb @@ -15,7 +15,7 @@ class Digraph # @h: Hash[Integer, (Integer|Float)?] # @result: Hash[Integer, Integer] - # @rbs sets: String + # @rbs sets: Array[Integer] # @rbs relation: Hash[Integer, Array[Integer]] # @rbs base_function: Hash[Integer, Integer] # @rbs return: void diff --git a/sig/generated/lrama/digraph.rbs b/sig/generated/lrama/digraph.rbs index 6a689c77..5758419e 100644 --- a/sig/generated/lrama/digraph.rbs +++ b/sig/generated/lrama/digraph.rbs @@ -15,11 +15,11 @@ module Lrama @result: Hash[Integer, Integer] - # @rbs sets: String + # @rbs sets: Array[Integer] # @rbs relation: Hash[Integer, Array[Integer]] # @rbs base_function: Hash[Integer, Integer] # @rbs return: void - def initialize: (String sets, Hash[Integer, Array[Integer]] relation, Hash[Integer, Integer] base_function) -> void + def initialize: (Array[Integer] sets, Hash[Integer, Array[Integer]] relation, Hash[Integer, Integer] base_function) -> void # @rbs () -> Hash[Integer, Integer] def compute: () -> Hash[Integer, Integer] From 3bbbec437ce292c43fc25d42c4dcea079a698349 Mon Sep 17 00:00:00 2001 From: yui-knk Date: Mon, 20 Jan 2025 15:21:36 +0900 Subject: [PATCH 4/5] Use type parameters for Digraph class --- lib/lrama/digraph.rb | 28 +++++++++++++++---------- sig/generated/lrama/digraph.rbs | 37 ++++++++++++++++++++------------- 2 files changed, 39 insertions(+), 26 deletions(-) diff --git a/lib/lrama/digraph.rb b/lib/lrama/digraph.rb index a37dc4b6..a3425280 100644 --- a/lib/lrama/digraph.rb +++ b/lib/lrama/digraph.rb @@ -3,21 +3,27 @@ module Lrama # Algorithm Digraph of https://dl.acm.org/doi/pdf/10.1145/69622.357187 (P. 625) + # + # @rbs generic X < Object -- Type of a member of `sets` + # @rbs generic Y < _Or -- Type of sets assigned to a member of `sets` class Digraph # TODO: rbs-inline 0.10.0 dosen't support instance variables. # Move these type declarations above instance variable definitions, once it's supported. # # @rbs! - # @sets: Array[Integer] - # @relation: Hash[Integer, Array[Integer]] - # @base_function: Hash[Integer, Integer] - # @stack: Array[Integer] - # @h: Hash[Integer, (Integer|Float)?] - # @result: Hash[Integer, Integer] + # interface _Or + # def |: (self) -> self + # end + # @sets: Array[X] + # @relation: Hash[X, Array[X]] + # @base_function: Hash[X, Y] + # @stack: Array[X] + # @h: Hash[X, (Integer|Float)?] + # @result: Hash[X, Y] - # @rbs sets: Array[Integer] - # @rbs relation: Hash[Integer, Array[Integer]] - # @rbs base_function: Hash[Integer, Integer] + # @rbs sets: Array[X] + # @rbs relation: Hash[X, Array[X]] + # @rbs base_function: Hash[X, Y] # @rbs return: void def initialize(sets, relation, base_function) @@ -40,7 +46,7 @@ def initialize(sets, relation, base_function) @result = {} end - # @rbs () -> Hash[Integer, Integer] + # @rbs () -> Hash[X, Y] def compute @sets.each do |x| next if @h[x] != 0 @@ -52,7 +58,7 @@ def compute private - # @rbs (Integer x) -> void + # @rbs (X x) -> void def traverse(x) @stack.push(x) d = @stack.count diff --git a/sig/generated/lrama/digraph.rbs b/sig/generated/lrama/digraph.rbs index 5758419e..d27a4660 100644 --- a/sig/generated/lrama/digraph.rbs +++ b/sig/generated/lrama/digraph.rbs @@ -2,31 +2,38 @@ module Lrama # Algorithm Digraph of https://dl.acm.org/doi/pdf/10.1145/69622.357187 (P. 625) - class Digraph - @sets: Array[Integer] + # + # @rbs generic X < Object -- Type of a member of `sets` + # @rbs generic Y < _Or -- Type of sets assigned to a member of `sets` + class Digraph[X < Object, Y < _Or] + interface _Or + def |: (self) -> self + end - @relation: Hash[Integer, Array[Integer]] + @sets: Array[X] - @base_function: Hash[Integer, Integer] + @relation: Hash[X, Array[X]] - @stack: Array[Integer] + @base_function: Hash[X, Y] - @h: Hash[Integer, (Integer | Float)?] + @stack: Array[X] - @result: Hash[Integer, Integer] + @h: Hash[X, (Integer | Float)?] - # @rbs sets: Array[Integer] - # @rbs relation: Hash[Integer, Array[Integer]] - # @rbs base_function: Hash[Integer, Integer] + @result: Hash[X, Y] + + # @rbs sets: Array[X] + # @rbs relation: Hash[X, Array[X]] + # @rbs base_function: Hash[X, Y] # @rbs return: void - def initialize: (Array[Integer] sets, Hash[Integer, Array[Integer]] relation, Hash[Integer, Integer] base_function) -> void + def initialize: (Array[X] sets, Hash[X, Array[X]] relation, Hash[X, Y] base_function) -> void - # @rbs () -> Hash[Integer, Integer] - def compute: () -> Hash[Integer, Integer] + # @rbs () -> Hash[X, Y] + def compute: () -> Hash[X, Y] private - # @rbs (Integer x) -> void - def traverse: (Integer x) -> void + # @rbs (X x) -> void + def traverse: (X x) -> void end end From 40e6f1b22a9751327354f53c1bbdbc37f3f25874 Mon Sep 17 00:00:00 2001 From: yui-knk Date: Mon, 20 Jan 2025 15:34:50 +0900 Subject: [PATCH 5/5] Fix a typo --- lib/lrama/digraph.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/lrama/digraph.rb b/lib/lrama/digraph.rb index a3425280..2161f304 100644 --- a/lib/lrama/digraph.rb +++ b/lib/lrama/digraph.rb @@ -7,7 +7,7 @@ module Lrama # @rbs generic X < Object -- Type of a member of `sets` # @rbs generic Y < _Or -- Type of sets assigned to a member of `sets` class Digraph - # TODO: rbs-inline 0.10.0 dosen't support instance variables. + # TODO: rbs-inline 0.10.0 doesn't support instance variables. # Move these type declarations above instance variable definitions, once it's supported. # # @rbs!