Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature.lambdas #34

Merged
merged 25 commits into from
Jul 12, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
structable params
  • Loading branch information
http://jneen.net/ committed Jun 16, 2022
commit fefa1bc729b4b382806690240579d9a95e5e77bd
26 changes: 23 additions & 3 deletions lib/dentaku/type/declared.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,30 @@ def self.declare(name, arity=0, &b)
end

class DeclaredType
def self.arity; raise "abstract"; end
def arity; self.class.arity; end
class << self
def inspect
"DeclaredType(:#{type_name}/#{arity})"
end

def arity; raise "abstract"; end
def type_name; raise "abstract"; end

def self.type_name; raise "abstract"; end
def structable(keys={})
@structable_keys ||= {}

keys.each do |k, v|
@structable_keys[k.to_s] = v
end

@structable_keys
end

def structable?
!!@structable_keys
end
end

def arity; self.class.arity; end
def type_name; self.class.type_name; end

attr_reader :args
Expand Down
8 changes: 7 additions & 1 deletion lib/dentaku/type/solver.rb
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,13 @@ def simplify_expr(constraint, expr)
expr.cases(
key_of: ->(struct, key) {
struct.cases(
param: -> { simplify_error!(expr) },
param: ->(name, args) {
as_struct = DECLARED_TYPES[name].structable

simplify_error!(expr, KeyError.new(constraint)) unless as_struct.key?(key)

Expression.from_sexpr(as_struct[key])
},
struct: ->(keys, types) {
idx = keys.find_index(key)
return simplify_error!(expr, KeyError.new(constraint)) if idx.nil?
Expand Down
25 changes: 25 additions & 0 deletions spec/type/declared_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
require 'spec_helper'

describe 'Declared Types' do
Dentaku::Type.declare(:fake_struct) do
structable(a: ':numeric', b: ':numeric')
end

Dentaku::Type.declare(:recursive) do
structable(a: ':recursive', b: ':numeric')
end

it 'destructs a structable type' do
checker = Dentaku::Type::StaticChecker.new(x: :fake_struct)
ast = Dentaku::Syntax.parse('x.a.a.a.a.b')

checker.check!(ast)
end

it 'recursively destructs a structable type' do
checker = Dentaku::Type::StaticChecker.new(x: :fake_struct)
ast = Dentaku::Syntax.parse('x.a.a.a.a.b')

checker.check!(ast)
end
end