-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhofl.pl
111 lines (91 loc) · 2.96 KB
/
hofl.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
:- use_module(library(main)).
:- use_module(library(optparse)).
:- use_module(src/abstree).
:- use_module(src/typing).
:- use_module(src/canonical).
:- use_module(src/latex).
%:- style_check(-singleton).
:- initialization(main, main).
main(Argv) :-
opts_spec(OptsSpec),
opt_parse(OptsSpec, Argv, Opts, PositionalArgs),
(
member(help(true), Opts) -> (opt_help(OptsSpec, Help), write("Usage: swipl hofl.pl <?opts> ?term \n"), write(Help));
member(mode(canonical), Opts) -> opts_canonical(Opts, PositionalArgs);
member(mode(typing), Opts) -> opts_type(Opts, PositionalArgs);
(opt_help(OptsSpec, Help),write("Usage: swipl hofl.pl <?opts> ?term"), write(Help))
)
.
opts_canonical(Opts, PositionalArgs) :-
(
member(infile(IF), Opts) -> (
\+var(IF) -> read_file_to_string(IF, T, []);
[T|_] = PositionalArgs
)
),
abstree(T, TERM),!,
write("Term: "),
write(TERM),
write("\n"),
canonic(TERM, C),
write("Canonical: "),
write(C).
%(member(outfile(OF), Opts) -> (\+var(OF), write_file(OF, C))).
opts_type(Opts, PositionalArgs) :-
(
member(infile(IF), Opts) -> (
\+var(IF) -> read_file_to_string(IF, T, []);
[T|_] = PositionalArgs
)
),
abstree(T, TERM),!,
inferType(TERM, _, TypedTerm),
write("Term: "),
write(TypedTerm),
member(outfile(OF), Opts) -> (
(\+var(OF), get_latex(TypedTerm, String), write_file(OF, String));
(get_latex(TypedTerm, String), format("~n"), write("Latex: "), write(String))
)
.
write_file(F, T) :-
open(F,write,OS),
writeln(OS, "\\documentclass[10pt]{article}"),
writeln(OS, "\\usepackage{mathtools}"),
writeln(OS, "\\begin{document}"),
writeln(OS, "\\["),
write(OS,T),
writeln(OS, "\\]"),
writeln(OS,"\\end{document}"),
close(OS).
% simple clause to try the typing only
ttype(X, TYPE, TypeTerm) :-
abstree(X, TERM),!,
write("Term: "),
write(TERM),
format("~n"),
inferType(TERM, TYPE, TypeTerm).
% simple clause to get the latex typing
llatex(X) :-
ttype(X, _, TypedTerm),
write(TypedTerm),
format("~n"),
get_latex(TypedTerm, S),
write(S).
% simple clause to get the canonical form of a term
get_canonical(T, C) :-
abstree(T, TERM),
canonic(TERM, C).
opts_spec(
[ [opt(mode), type(atom), default('SCAN'),
shortflags([m]), longflags(['mode'] ),
help([ 'Modes are:'
, ' canonical: derive the canonical form'
, ' typing: assigns types to a term'])]
, [opt(outfile), meta('OFILE'), type(atom),
shortflags([o]), longflags(['output-file']),
help('write output (typing) to OFILE')]
, [opt(infile), meta('IFILE'), type(atom),
shortflags([f]), help('read term from IFILE')]
, [opt(help), type(boolean), default(false),
longflags(['help'] ),shortflags([h]), help('Help')]
]).