標準ライブラリ非決定論的・除外対象モジュール カバレッジ
2026-05-22生成。決定論的グループ文書(20260522-stdlib-deterministic-module-coverage.md)の対になる文書。
Constant[T]への折りたたみが原則不可・あるいは実益が薄いモジュールを収録する。
| 記号 | 意味 |
|---|---|
| 🚫 | 対象外(副作用・非決定的・型精度向上がnegligible) |
| 🔲 | 限定的に精度向上の余地あり(返値Refinementなど) |
| 🔷 | RBSで十分な精度が出ている |
以下のいずれかに該当するモジュールをこのグループとする。
- 副作用: ファイルシステム・ネットワーク・OSプロセスを変更する
- 非決定論: 同一引数でも実行ごとに異なる出力を返す(乱数・タイムスタンプ等)
- 返値がuntyped:
Marshal.loadのようにリテラル入力でも返値型が静的に不明 - 折りたたみ実益が薄い: 計算は決定論的でも、実際の定数値を型推論に使う場面がない(Base64 / Digest)
1. SecureRandom
Section titled “1. SecureRandom”SecureRandom.methods - Module.methods → 12メソッド(Ruby 4.0.5):
:alphanumeric, :base64, :bytes, :gen_random, :hex, :rand, :random_bytes, :random_number, :urlsafe_base64, :uuid, :uuid_v4, :uuid_v7
除外理由: OSの乱数源から読み取るため、同一呼び出しでも必ず異なる値を返す。
hex(n)の返値型はString(non-empty-string Refinement追加は可能)、
uuid / uuid_v4 / uuid_v7の返値型はRBSでString済み。
| メソッド | 返値型 | 状態 | 備考 |
|---|---|---|---|
hex(n) | String (hex) | 🚫 | 非決定的。RBSでString。 |
base64(n) | String | 🚫 | 非決定的。 |
urlsafe_base64(n) | String | 🚫 | 非決定的。 |
random_bytes(n) / bytes(n) | String (binary) | 🚫 | 非決定的。 |
gen_random(n) | String | 🚫 | 非決定的(内部実装用)。 |
rand / random_number | Float | Integer | 🚫 |
uuid / uuid_v4 | String | 🚫 | 非決定的。 |
uuid_v7 | String | 🚫 | 非決定的(タイムスタンプ成分含む)。 |
alphanumeric(n) | String | 🚫 | 非決定的。 |
2. Random
Section titled “2. Random”Random.methods - Class.methods → 7メソッド:
:bytes, :new_seed, :rand, :random_number, :seed, :srand, :urandom
除外理由:クラスメソッドはグローバル乱数状態に依存。
Random.new(seed)インスタンスはseed固定で決定論的だが、
インスタンスメソッドの精度向上はConstantFolding対象外(インスタンス状態を持つ)。
| メソッド | 状態 | 備考 |
|---|---|---|
rand / random_number | 🚫 | グローバル乱数。 |
bytes(n) | 🚫 | 非決定的。 |
srand(seed) | 🚫 | 副作用(グローバル状態変更)。 |
seed / new_seed | 🚫 | 非決定的。 |
urandom(n) | 🚫 | OS乱数源。 |
3. FileUtils
Section titled “3. FileUtils”FileUtils.methods - Module.methods → 57メソッド(代表のみ抜粋)。
除外理由:ファイルシステムへの副作用専用モジュール。
戻り値は操作対象パスやnilであり、型精度向上の意味がない。
| カテゴリ | メソッド例 | 状態 |
|---|---|---|
| ファイルコピー / 移動 | cp, cp_r, mv, install | 🚫 |
| ファイル削除 | rm, rm_f, rm_r, rm_rf | 🚫 |
| ディレクトリ操作 | mkdir, mkdir_p, rmdir | 🚫 |
| リンク | ln, ln_s, ln_sf, ln_sr | 🚫 |
| パーミッション | chmod, chmod_R, chown, chown_R | 🚫 |
| 比較 | cmp / compare_file, identical? | 🚫 |
| ユーティリティ | pwd / getwd, cd / chdir, touch | 🚫 |
| メタ情報 | commands, options, options_of, have_option? | 🚫 |
4. Marshal
Section titled “4. Marshal”Marshal.methods - Module.methods → :dump, :load, :restore
除外理由(ユーザー決定): dumpは決定論的(同一オブジェクトで同一バイト列)だが、
生成されるバイト列をリテラル型として使う場面がない。
load / restoreはバイナリから任意のRubyオブジェクトを復元するため返値型がuntyped(静的に推論不可)。
決定論性はあっても型推論上の実益がMarshalには存在しない。
| メソッド | 状態 | 備考 |
|---|---|---|
dump(obj) | 🚫 | 決定論的だが返値(binary String)を定数として使う場面なし。 |
load(str) / restore(str) | 🚫 | 返値型がuntyped。 |
GC.methods - Module.methods → 20メソッド。
除外理由: GC操作はランタイム状態(ヒープ使用量・オブジェクト生存数等)を返す。
同一コードでも実行環境・タイミングで値が変わる。
| カテゴリ | メソッド例 | 状態 |
|---|---|---|
| 統計取得 | count, stat, stat_heap, total_time | 🚫 |
| GC実行 / 制御 | start, compact, disable, enable | 🚫 |
| 設定 | config, measure_total_time, stress | 🚫 |
| 検証 | verify_compaction_references, verify_internal_consistency | 🚫 |
6. Base64(悩ましいグループ)
Section titled “6. Base64(悩ましいグループ)”Ruby 4.0ではbundled gemに降格(require "base64"が必要)。
Base64.methods - Module.methods → :decode64, :encode64, :strict_decode64, :strict_encode64, :urlsafe_decode64, :urlsafe_encode64
除外理由(決定論グループに入れない理由):
| 観点 | 内容 |
|---|---|
| 決定論性 | ✓ 同一入力で常に同一出力 |
| 定数折りたたみの実益 | ✗ encode64("hello") → "aGVsbG8=\n"を定数値として型推論に使う場面がない |
| 返値型精度 | △ 返値は常にString(RBS済み)。non-empty-string Refinementは追加可能だが効果が小さい |
| bundled gem問題 | require "base64"が必要(Ruby 4.0以降)。受信者型の認識がgems状況に依存 |
将来の昇格条件: non-empty-string / base64-string専用Refinementを導入する場合、
決定論グループに移動してstrict_encode64 / urlsafe_encode64へRefinement付与を検討。
| メソッド | 状態 | 備考 |
|---|---|---|
encode64(str) | 🚫 | 決定論的。折りたたみ実益なし。改行(\n)含む点が特殊。 |
strict_encode64(str) | 🚫 | 改行なし版。同上。 |
urlsafe_encode64(str) | 🚫 | URL-safe版。同上。 |
decode64(str) | 🚫 | デコード。返値binary String。 |
strict_decode64(str) | 🚫 | strict版。不正入力でArgumentError。 |
urlsafe_decode64(str) | 🚫 | URL-safe版。同上。 |
7. Digest(悩ましいグループ)
Section titled “7. Digest(悩ましいグループ)”Digest.methods - Module.methods → :hexencode(Digestモジュールレベルは1関数のみ)。
実用上はDigest::MD5.hexdigest(str) / Digest::SHA256.hexdigest(str)等のクラスメソッドが対象。
除外理由(決定論グループに入れない理由):
| 観点 | 内容 |
|---|---|
| 決定論性 | ✓ 暗号学的ハッシュ関数は決定論的 |
| 定数折りたたみの実益 | ✗ テスト用リテラル入力に実際のハッシュ値が出ても型推論上使えない |
| 返値型精度 | △ hexdigest → 固定長hex文字列(MD5: 32文字, SHA256: 64文字)。専用Refinement hex-string / md5-hex-stringを定義すれば添付可能 |
| 返値長 | Digestアルゴリズムごとに長さが異なる。汎用Refinementでは長さ情報が失われる |
将来の昇格条件: hex-string / md5-hex-string / sha256-hex-string等の長さ付きRefinementを
imported-built-in-types.mdに追加する場合、決定論グループへ移動してRefinement付与を検討。
| メソッド / クラス | 状態 | 備考 |
|---|---|---|
Digest.hexencode(str) | 🚫 | 入力文字列をhex表現に変換。定数折りたたみ実益なし。 |
Digest::MD5.hexdigest(str) | 🚫 | 32文字hex文字列。決定論的だが値を使わない。 |
Digest::SHA1.hexdigest(str) | 🚫 | 40文字hex文字列。同上。 |
Digest::SHA256.hexdigest(str) | 🚫 | 64文字hex文字列。同上。 |
Digest::SHA512.hexdigest(str) | 🚫 | 128文字hex文字列。同上。 |
Digest::*#digest(str) (binary) | 🚫 | バイナリ文字列。 |
Digest::*#update / << | 🚫 | 破壊的状態蓄積。 |
サマリ: 非対象グループの型精度向上シナリオ
Section titled “サマリ: 非対象グループの型精度向上シナリオ”現状は全て 🚫 だが、以下のシナリオで一部が昇格しうる。
| シナリオ | 対象 | 必要な前提 |
|---|---|---|
non-empty-string Refinement拡充 | SecureRandom.uuid, SecureRandom.hex(n > 0), Base64.encode64, Digest::*.hexdigest | Refinement型としてnon-empty-stringが充実し、返値に自動付与するルールが整備される |
hex-string専用Refinement | SecureRandom.hex, Digest::*.hexdigest | imported-built-in-types.mdへの新規Refinement追加 + length情報付与 |
| UUID文字列型 | SecureRandom.uuid / uuid_v4 / uuid_v7 | uuid-string Refinement(36文字、特定フォーマット) |
© 2026 TypedDuck. Licensed under CC BY-SA 4.0.