From 566bb84e3ac629a863cbede51a400552f6d03b5d Mon Sep 17 00:00:00 2001 From: Francisco Coelho Date: Wed, 10 Jul 2024 01:29:36 +0100 Subject: [PATCH] Done: asp_program --- PASPProc.jl | 106 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ drafts.jl | 67 ------------------------------------------------------------------- drafts_PASPProc.jl | 16 ++++++++++++++++ symb-ltxify--drafts.jl | 67 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 189 insertions(+), 67 deletions(-) create mode 100644 PASPProc.jl delete mode 100644 drafts.jl create mode 100644 drafts_PASPProc.jl create mode 100644 symb-ltxify--drafts.jl diff --git a/PASPProc.jl b/PASPProc.jl new file mode 100644 index 0000000..d0e43a6 --- /dev/null +++ b/PASPProc.jl @@ -0,0 +1,106 @@ +module PASPProc + +include("../asplang/ASPLang.jl") +using ASPLang + +export tree_string, + filter_tree, + filter_children, + has_child, + node_findreplace, + has_annotation_P, + repl_annotation_disjunction, + derived + +tree_string(node::AnonymousVariable; level=0, indent="\t") = "$(indent^level)Anonymous: $(node.args |> string)" +tree_string(node::Functor; level=0, indent="\t") = "$(indent^level)Functor: $(node.args |> string)" +tree_string(node::Annotation; level=0, indent="\t") = "$(indent^level)Annotation: $(node.args |> string)" +tree_string(node::Variable; level=0, indent="\t") = "$(indent^level)Variable: $(node.args |> string)" +tree_string(node::StringConstant; level=0, indent="\t") = "$(indent^level)String: $(node.args |> string)" +tree_string(node::SymbolicConstant; level=0, indent="\t") = "$(indent^level)Symbol: $(node.args |> string)" +tree_string(node::NumberConstant; level=0, indent="\t") = "$(indent^level)Number: $(node.args |> string)" +#tree_print(node::Terminal; level=0, indent="\t") = node |> typeof |> string +function tree_string(node::ASPLang.NonTerminal; level=0, indent="\t") + node_head = node |> typeof |> string + sub_results = [ "$(indent^level)$node_head" ] + if isa(node.args, AbstractArray) && length(node.args) > 0 + for child in node.args + child_tree = if isa(child, ASPLang.Node) + tree_string(child, level = level + 1, indent=indent) + else + "$(indent^(level+1))$(child |> string)" + end + push!(sub_results, child_tree) + end + else + push!(sub_results, tree_string(node.args, level=level+1, indent=indent)) + end + join(sub_results, "\n") +end +tree_string(node; level=0, indent="\t") = "$(indent^level)$(node |> typeof |> string):$(node |> string)" + + +function filter_tree(node::NonTerminal, prop) + results = if prop(node) + [node] + else + [] + end + if isa(node.args, AbstractArray) + for d in node.args + append!(results, filter_tree(d, prop)) + end + else + if prop(node.args) + push!(results, node.args) + end + end + results +end + +function filter_children(node::NonTerminal, prop) + if isa(node.args, AbstractArray) + filter(prop, node.args) + else + if prop(node.args) + node.args + else + nothing + end + end +end + +function has_child(prop) + node::NonTerminal -> any(prop, node.args) +end + +function node_findreplace(node::NonTerminal, prop, action) + if prop(node) + action(node) + elseif isa(node.args, AbstractArray) + new_args = [] + for arg in node.args + push!(new_args, node_findreplace(arg, prop, action)) + end + typeof(node)(new_args) + else + node + end +end + +has_annotation_P = n -> has_child(isa(n, ASPLang.Annotation)) +function repl_annotation_disjunction(n::NonTerminal) + a = [xi for xi in n.args if !isa(xi, Annotation)][1] + Head( + Disjunction( + [a, Negated(a)] + )) +end + +function derived(p::ASPLang.Program) + node_findreplace(p, + has_annotation_P, + repl_annotation_disjunction) +end + +end # PASPproc \ No newline at end of file diff --git a/drafts.jl b/drafts.jl deleted file mode 100644 index ec82458..0000000 --- a/drafts.jl +++ /dev/null @@ -1,67 +0,0 @@ -using Symbolics -using Latexify - -# # γ = 0.5 -# no_s = [ -# 0 , -# 23 , -# 614 , -# 165 , -# 169 , -# 0 , -# 0 , -# 4 , -# 25 , -# ] - -# γ = 0.8 -no_s = [ - 0 , - 28 , - 632 , - 246 , - 59 , - 0 , - 0 , - 5 , - 27 , -] -pr_s = (x -> x // 1000).(no_s) - -@variables θ -num_e = [ - 0 , - 0 , - 7 , - 3 * θ , - 3 * (1 - θ) , - 0 , - 0 , - 3 , - 10 , -] -pr_e = (x -> x // 23).(num_e) - -target = expand(sum( (x -> x^2).(pr_s - pr_e) )) -println(latexify(target)) - -# using Plots -# g(t) = (20869963/66125000) + (477/52900)*t + (18/529)*(t^2) -# t = 0:0.1:1 -# plot(t, g.(t)) - - - -function solve2(a, b, c) - delta = sqrt(Complex(b^2 - 4 * a * c)) - return ( (-b - delta)/(2*a), (-b + delta)/(2*a) ) -end - -# # g = target ~ 0 - -a = float(18//529) -b = -float(21903//264500) -c = float(188207311//529000000) - -println("a: $a b: $b c: $c") -println("̂θ = $(-b/a)") diff --git a/drafts_PASPProc.jl b/drafts_PASPProc.jl new file mode 100644 index 0000000..3930a52 --- /dev/null +++ b/drafts_PASPProc.jl @@ -0,0 +1,16 @@ +include("../asplang/ASPLang.jl") +include("PASPProc.jl") + +using .ASPLang +using .PASPProc + + +root_path = "/home/fc/sci/projetos/zugzwang/code/asplang" +asp_file = "sbf.lp" +src = read("$(root_path)/$(asp_file)", String) + +println(methods(tree_string)) +# e = parse_asp(src, asp_grammar) +# println(isa(e, NonTerminal)) +# t = tree_string(e) +# println(t) \ No newline at end of file diff --git a/symb-ltxify--drafts.jl b/symb-ltxify--drafts.jl new file mode 100644 index 0000000..ec82458 --- /dev/null +++ b/symb-ltxify--drafts.jl @@ -0,0 +1,67 @@ +using Symbolics +using Latexify + +# # γ = 0.5 +# no_s = [ +# 0 , +# 23 , +# 614 , +# 165 , +# 169 , +# 0 , +# 0 , +# 4 , +# 25 , +# ] + +# γ = 0.8 +no_s = [ + 0 , + 28 , + 632 , + 246 , + 59 , + 0 , + 0 , + 5 , + 27 , +] +pr_s = (x -> x // 1000).(no_s) + +@variables θ +num_e = [ + 0 , + 0 , + 7 , + 3 * θ , + 3 * (1 - θ) , + 0 , + 0 , + 3 , + 10 , +] +pr_e = (x -> x // 23).(num_e) + +target = expand(sum( (x -> x^2).(pr_s - pr_e) )) +println(latexify(target)) + +# using Plots +# g(t) = (20869963/66125000) + (477/52900)*t + (18/529)*(t^2) +# t = 0:0.1:1 +# plot(t, g.(t)) + + + +function solve2(a, b, c) + delta = sqrt(Complex(b^2 - 4 * a * c)) + return ( (-b - delta)/(2*a), (-b + delta)/(2*a) ) +end + +# # g = target ~ 0 + +a = float(18//529) +b = -float(21903//264500) +c = float(188207311//529000000) + +println("a: $a b: $b c: $c") +println("̂θ = $(-b/a)") -- libgit2 0.21.2