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

Add operator name canonicalization #54

Open
wants to merge 15 commits into
base: main
Choose a base branch
from
18 changes: 9 additions & 9 deletions src/acset.jl
Original file line number Diff line number Diff line change
Expand Up @@ -271,7 +271,7 @@ function contract_operators!(d::SummationDecapode; allowable_ops::Set{Symbol} =
chains = find_chains(d, allowable_ops = allowable_ops)
filter!(x -> length(x) != 1, chains)
for chain in chains
add_part!(d, :Op1, src=d[:src][first(chain)], tgt=d[:tgt][last(chain)], op1=Vector{Symbol}(d[:op1][chain]))
add_part!(d, :Op1, src=d[first(chain), :src], tgt=d[last(chain), :tgt], op1=Vector{Symbol}(d[:op1][chain]))
end
rem_parts!(d, :Op1, sort!(vcat(chains...)))
remove_neighborless_vars!(d)
Expand Down Expand Up @@ -424,7 +424,7 @@ function apply_inference_rule_op1!(d::SummationDecapode, op1_id, rule)

score_src = (rule.src_type == type_src)
score_tgt = (rule.tgt_type == type_tgt)
check_op = (d[op1_id, :op1] in rule.op_names)
check_op = (deca_canon_op1(d, op1_id) in rule.op_names)

if(check_op && (score_src + score_tgt == 1))
mod_src = safe_modifytype!(d, d[op1_id, :src], rule.src_type)
Expand All @@ -443,7 +443,7 @@ function apply_inference_rule_op2!(d::SummationDecapode, op2_id, rule)
score_proj1 = (rule.proj1_type == type_proj1)
score_proj2 = (rule.proj2_type == type_proj2)
score_res = (rule.res_type == type_res)
check_op = (d[op2_id, :op2] in rule.op_names)
check_op = (deca_canon_op2(d, op2_id) in rule.op_names)

if(check_op && (score_proj1 + score_proj2 + score_res == 2))
mod_proj1 = safe_modifytype!(d, d[op2_id, :proj1], rule.proj1_type)
Expand Down Expand Up @@ -521,21 +521,21 @@ Resolve function overloads based on types of src and tgt.
"""
function resolve_overloads!(d::SummationDecapode, op1_rules::Vector{NamedTuple{(:src_type, :tgt_type, :resolved_name, :op), NTuple{4, Symbol}}}, op2_rules::Vector{NamedTuple{(:proj1_type, :proj2_type, :res_type, :resolved_name, :op), NTuple{5, Symbol}}})
for op1_idx in parts(d, :Op1)
src = d[:src][op1_idx]; tgt = d[:tgt][op1_idx]; op1 = d[:op1][op1_idx]
src_type = d[:type][src]; tgt_type = d[:type][tgt]
src = d[op1_idx, :src]; tgt = d[op1_idx, :tgt]
src_type = d[src, :type]; tgt_type = d[tgt, :type]
for rule in op1_rules
if op1 == rule[:op] && src_type == rule[:src_type] && tgt_type == rule[:tgt_type]
if d[op1_idx, :op1] == rule[:op] && src_type == rule[:src_type] && tgt_type == rule[:tgt_type]
d[op1_idx, :op1] = rule[:resolved_name]
break
end
end
end

for op2_idx in parts(d, :Op2)
proj1 = d[:proj1][op2_idx]; proj2 = d[:proj2][op2_idx]; res = d[:res][op2_idx]; op2 = d[:op2][op2_idx]
proj1_type = d[:type][proj1]; proj2_type = d[:type][proj2]; res_type = d[:type][res]
proj1 = d[op2_idx, :proj1]; proj2 = d[op2_idx, :proj2]; res = d[op2_idx, :res]
proj1_type = d[proj1, :type]; proj2_type = d[proj2, :type]; res_type = d[res, :type]
for rule in op2_rules
if op2 == rule[:op] && proj1_type == rule[:proj1_type] && proj2_type == rule[:proj2_type] && res_type == rule[:res_type]
if d[op2_idx, :op2] == rule[:op] && proj1_type == rule[:proj1_type] && proj2_type == rule[:proj2_type] && res_type == rule[:res_type]
d[op2_idx, :op2] = rule[:resolved_name]
break
end
Expand Down
4 changes: 3 additions & 1 deletion src/deca/Deca.jl
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,10 @@ using Catlab

import ..infer_types!, ..resolve_overloads!

export normalize_unicode, varname, infer_types!, resolve_overloads!, typename, spacename, recursive_delete_parents, recursive_delete_parents!, unicode!, op1_res_rules_1D, op2_res_rules_1D, op1_res_rules_2D, op2_res_rules_2D, op1_inf_rules_1D, op2_inf_rules_1D, op1_inf_rules_2D, op2_inf_rules_2D, vec_to_dec!
export normalize_unicode, varname, infer_types!, resolve_overloads!, typename, spacename, recursive_delete_parents, recursive_delete_parents!, unicode!, op1_res_rules_1D, op2_res_rules_1D, op1_res_rules_2D, op2_res_rules_2D, op1_inf_rules_1D, op2_inf_rules_1D, op1_inf_rules_2D, op2_inf_rules_2D, vec_to_dec!,
get_canon_name, deca_canon_op1, deca_canon_op2

include("deca_op_names.jl")
include("deca_acset.jl")
include("deca_visualization.jl")

Expand Down
Loading
Loading