-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathhelpers.pl
125 lines (101 loc) · 3.5 KB
/
helpers.pl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
:- doc_server(4000). % Start PlDoc at port 4000
:- portray_text(true). % Enable portray of strings
:- use_module(library(lists)).
:- use_module(library(doc_latex)).
:- use_module(automata).
:- use_module(automata(convert)).
:- use_module(automata(printing)).
:- doc_collect(true).
concatentated_member(L1, L2, L3) :-
member(M1, L1), member(M2, L2),
string_concat(M1, M2, L3).
concatentated(L1, L2, L3) :-
findall(X, concatentated_member(L1, L2, X), X),
list_to_set(X, L3).
test_automata(X) :-
regex_to_nfa(`x(y+x)*+z`, X),
nfa_table(X, Y),
format_table(Y).
test_to_string :-
phrase('automata/parser':gexps(Parsed), `x(y+x)*+z`),
regex_to_string(Parsed, S),
format('Regex: ~w~n', [S]).
test_nfa_to_dfa(Dfa) :-
regex_to_nfa(`x(y+x)*+z`, Nfa),
nfa_to_dfa(Nfa, Dfa),
format_table(Dfa).
test_match :-
match_regex(`x(y+x)*+z`, `xyyxxxyxxy`).
test_mismatch :-
match_regex(`x(y+x)*+z`, `xyyxxxyxxyz`).
test_smatch_suffix(Suffix) :-
match_suffix_regex(`x(y+x)*+z`, `xyyxxxyxxyzab`, Suffix).
test_reacheable_epsilon(States) :-
regex_to_nfa(`x(y+x)*+z`, X),
nfa_table(X, Y),
'automata/convert':reacheable_epsilon(3, x, Y, States).
test_match_all(Matches) :-
findall(X, match_all_regex(`x(y+x)*+z`, `xyyxxxyxxyz`, X), Matches).
test_dfa_to_regex(Regex) :-
regex_to_nfa(`x(y+x)*+z`, X),
nfa_to_dfa(X, Y),
dfa_to_regex(Y, Regex).
test_invert(Regex) :-
invert_regex(`x(y+x)*+z`, Regex).
gen_doc :-
doc_latex(['automata',
'automata/ast',
'automata/convert',
'automata/parser',
'automata/printing',
'automata/utils'],
'automata-doc.tex',
[stand_alone(false),
section_level(subsection)]).
largest_subseq_sum(Seq, Max) :-
findall(S, (append([_, X, _], Seq), sum_list(X, S)), Subs),
max_list(Subs, Max).
% ?- largest_subseq_sum([3, -2, 5, -9, 7, 1], X).
% X = 8.
replace_tex(Out) -->
[], { Out = "" } ;
"*", replace_tex(Y), { string_concat("^*", Y, Out) } ;
[603], replace_tex(Y), { string_concat("\\epsilon", Y, Out) } ;
[X], replace_tex(Y), { text_to_string([X], Xs), string_concat(Xs, Y, Out) }.
assignment_12a :-
invert_regex(`x(y+x)*+z`, Regex),
string_codes(Regex, Codes),
phrase(replace_tex(X), Codes),
format('$$~w$$', [X]).
test_union_optimize(Optimized) :-
phrase('automata/parser':gexps(Parsed), `((((z+y)z+((z+y)y+(z+y)x))+((z+y)+(((z+y)z+((z+y)y+(z+y)x))((z+(y+x)))*+((xy+xx)z+((xy+xx)((y+x))*+xz(z+(y+x)))))))+(((xy+xx)zz+((xy+xx)zy+((xy+xx)zx+(xz(z+(y+x))z+(xz(z+(y+x))y+xz(z+(y+x))x)))))(ɛ+((z+(y+x)))*)+ɛ))`),
'automata/ast':optimize_unions(Parsed, Optimized),
format('Optimized: ~w~n', [Optimized]).
compact(N, [_], [M]) :- M is N + 1.
compact(N, [X, X | Xs], Zs) :-
M is N + 1,
compact(M, [X | Xs], Zs).
compact(N, [X, Y | Xs], [M | Zs]) :-
dif(X, Y),
M is N + 1,
compact(0, [Y | Xs], Zs).
compact(In, Out) :-
compact(0, In, Out).
five_dice_rolls(Results) :-
Die = [1, 2, 3, 4, 5, 6],
findall(X,
(member(A, Die),
member(B, Die),
member(C, Die),
member(D, Die),
member(E, Die),
sum_list([A, B, C, D, E], X)),
Rolls),
sort(0, @=<, Rolls, Sorted),
compact(Sorted, Compact),
numlist(5, 30, Seq),
pairs_keys_values(Results, Compact, Seq).
test_dfa_minimize(X) :-
regex_to_nfa(`x(y+x)*+z`, Nfa),
nfa_to_dfa(Nfa, Dfa),
dfa_minimize(Dfa, X).