1+ export @debug
2+ # reshape
3+
4+ function bracketsplit (str)
5+ parts = []
6+ part_start = 1
7+ bracket_balance = 0
8+ for idx in eachindex (str)
9+ bracket_balance += @match str[idx] begin
10+ ' (' => - 1
11+ ' )' => 1
12+ _ => 0
13+ end
14+ if bracket_balance == 0
15+ push! (parts, str[part_start: idx])
16+ part_start = idx+ 1
17+ end
18+ end
19+ parts
20+ end
21+
22+ function argumentsplit (str)
23+ parts = strip .(bracketsplit (str))
24+ comma = findfirst (parts, " ," )
25+ if comma == 0
26+ [str]
27+ else
28+ arguments = [join (parts[1 : comma- 1 ])]
29+ while comma != 0
30+ next_comma = findnext (parts, " ," , comma+ 1 )
31+ if next_comma == 0
32+ push! (arguments, join (parts[comma+ 1 : end ]))
33+ else
34+ push! (arguments, join (parts[comma+ 1 : next_comma- 1 ]))
35+ end
36+ comma = next_comma
37+ end
38+ arguments
39+ end
40+ end
41+
42+ function strip_and_remove_surrounding_brackets (str)
43+ str = strip (str)
44+ while length (bracketsplit (str)) == 1 && str[1 ] == ' ('
45+ str = str[2 : end - 1 ]
46+ end
47+ str
48+ end
49+
50+ function powerset (x)
51+ result = [Set ()]
52+ # println("collect(x) ", collect(x))
53+ for element in collect (x), idx = 1 : length (result)
54+ # println("element ", element)
55+ # println("idx ", idx)
56+ newsubset = union (result[idx], Set ([element]))
57+ push! (result, newsubset)
58+ end
59+ Set (result)
60+ end
61+
62+ # Currently, Julia's multiple dispatch system only dispatches on the expression type for
63+ # macros (which is most of the times Expr or Symbol). To do multiple dispatch here, we
64+ # therefore have to work around that limitation by using the hack below. This might change
65+ # if https://github.com/JuliaLang/julia/issues/20929 gets addressed.
66+ macro applyrecursively (functionexpr, objectexpr, objecttype)
67+ typemapping = Dict (
68+ :CNF => :collection ,
69+ :Clause => :collection ,
70+ :Conjunction => :monotone_fields ,
71+ :Negation => :monotone_fields ,
72+ )
73+ dispatch_applyrecursively (functionexpr, objectexpr, Val{typemapping[objecttype]})
74+ end
75+
76+ function dispatch_applyrecursively (functionexpr, objectexpr,
77+ objecttype:: Type{Val{:collection}} )
78+ fsymbol, fargs = functionexpr. args[1 ],
79+ functionexpr. args[2 : end ]
80+ quote
81+ obj = $ (esc (objectexpr))
82+ typeof (obj)(
83+ [$ fsymbol ([arg == :_ ? element : arg
84+ for arg in [$ (esc .(fargs)... )]]. .. )
85+ for element in obj]
86+ )
87+ end
88+ end
89+
90+ # function dispatch_applyrecursively(functionexpr, objectexpr,
91+ # objecttype::Type{Val{:monotone_fields}})
92+ # fsymbol, fargs = functionexpr.args[1],
93+ # functionexpr.args[2:end]
94+ # quote
95+ # typeof($(esc(objectexpr)))(
96+ # [$fsymbol(
97+ # [arg == :_ ? field : arg
98+ # for arg in $(esc(fargs))]...)
99+ # for field in getfield.($(esc(objectexpr)), fieldnames($(esc(objectexpr))))]...)
100+ # end
101+ # end
102+
103+ macro debug (variable)
104+ Expr (:call ,
105+ :println ,
106+ Expr (:call ,
107+ :string ,
108+ Expr (:quote , variable)),
109+ " " ,
110+ esc (variable),
111+ " \n "
112+ )
113+ end
114+ print (args... ) = println (args... )
115+
116+ let nextfreesymbol = 0
117+ global getnextfreesymbol
118+ global resetfreesymbols
119+ function getnextfreesymbol ()
120+ nextfreesymbol += 1
121+ string (" ##" , nextfreesymbol)
122+ end
123+ function resetfreesymbols ()
124+ nextfreesymbol = 0
125+ end
126+ end
127+
128+ # function reshape(formula::String, form::FormulaForm)
129+ # form == string ? String(Formula(formula)) : reshape(Formula(formula))
130+ # end
131+
132+ # function reshape(formula::Formula, form::FormulaForm)
133+ # @match form begin
134+ # string => String(formula)
135+ # formula => formula
136+ # quantifiedvariables => get_quantified_variables_form(formula)
137+ # prenex => get_prenex_normal_form(formula)
138+ # skolem => get_prenex_normal_form(
139+ # get_skolem_normal_form(formula)
140+ # )
141+ # removedquantifier => get_removed_quantifier_form(formula)
142+ # conjunctive => get_conjunctive_normal_form(formula)
143+ # resolutionready => get_conjunctive_normal_form(
144+ # get_removed_quantifier_form(
145+ # get_skolem_normal_form(
146+ # get_prenex_normal_form(
147+ # get_quantified_variables_form(
148+ # Formula(formula)))))
149+ # )
150+ # end
151+ # end
0 commit comments