-
Notifications
You must be signed in to change notification settings - Fork 0
/
mkCollectArguments.sh
executable file
·169 lines (161 loc) · 4.73 KB
/
mkCollectArguments.sh
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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
#!/bin/bash
cat << EOF
:- module sentence_collectArguments.
:- interface.
:- import_module mrs.
:- import_module multi_map.
:- import_module sentence_predicates.
:- import_module sentence_types.
:- pred collectArguments(multi_map(mrs_rel_handle,{mrs_rel_handle,string,string,preds}),
mrs_rel_handle,
preds,
multi_map(mrs_types, mrs_rel_handle),
multi_map(mrs_types, mrs_rel_handle)).
:- mode collectArguments(in,in,in,in,out) is det.
:- implementation.
:- import_module require.
:- import_module solutions.
:- import_module list.
:- import_module calls.
:- import_module io.
:- import_module unsafe.
:- pragma promise_pure(collectArguments/5).
collectArguments(RelMap,RelHandleIn,Pred,ArgMapIn,ArgMapOut) :-
EOF
sed -n '/mrs/s/.*pred_\([a-z0-9_]*\)(pred(\(mrs_rel_handle,[^)]*\)))/\1\,\2/p' sentence_predicates.m | tr -d ' ' | tr -d . > _predicate_table
declare -a ArgAry
declare -a WrapExprs
declare -a Types
for line in `cat _predicate_table`; do
set -- `echo $line | tr , ' '`
pred=$1
shift
IndivPos=0
InstPos=0
RelHandlePos=0
RstrHandlePos=0
BodyHandlePos=0
CargPos=0
UnknownPos=0
delim=""
Args=""
ArgPos=0
EventPos=0
echo " ${lineDelim}if pred_${pred}(_) = Pred then"
while test -n "$1"; do
Types[${ArgPos}]=$1
case $1 in
mrs_indiv)
Var=Indiv${IndivPos}
Rhs=$Var
IndivPos=$((${IndivPos} + 1))
WrapExpr="wrap_indiv(${Var})"
;;
mrs_inst)
Var=Inst${InstPos}
Rhs=$Var
InstPos=$((${InstPos} + 1))
WrapExpr="wrap_inst(${Var})"
;;
mrs_event)
Var=Event${EventPos}
Rhs=$Var
EventPos=$((${EventPos} + 1))
WrapExpr="wrap_event(${Var})"
;;
mrs_rel_handle)
Var=RelHandle${RelHandlePos}
Rhs=$Var
RelHandlePos=$((${RelHandlePos} + 1))
WrapExpr="wrap_rel_handle(${Var})"
;;
mrs_rstr_handle)
Var="mrs_rstr_handle(RstrHandle${RstrHandlePos})"
Rhs="mrs_rel_handle(RstrHandle${RstrHandlePos})"
RstrHandlePos=$((${RstrHandlePos} + 1))
WrapExpr="wrap_rel_handle(mrs_rel_handle(${Rhs}))"
Types[${ArgPos}]=mrs_rel_handle
;;
mrs_body_handle)
Var="mrs_body_handle(BodyHandle${BodyHandlePos})"
Rhs="mrs_rel_handle(BodyHandle${BodyHandlePos})"
BodyHandlePos=$((${BodyHandlePos} + 1))
WrapExpr="wrap_rel_handle(mrs_rel_handle(${Rhs}))"
Types[${ArgPos}]=mrs_rel_handle
;;
mrs_carg)
Var=Carg${CargPos}
Rhs=$Var
CargPos=$((${CargPos} + 1))
WrapExpr="wrap_carg(${Var})"
;;
mrs_unknown)
Var=Unk${UnknownPos}
Rhs=$Var
UnknownPos=$((${UnknownPos} + 1))
WrapExpr="wrap_unknown(${Var})"
;;
*)
exit 1
;;
esac
Args="${Args}${delim}${Var}"
ArgAry[${ArgPos}]=${Var}
WrapExprs[${ArgPos}]=${WrapExpr}
Vars[${ArgPos}]=$Rhs
delim=","
shift
ArgPos=$((${ArgPos} + 1))
done
echo " solutions(pred({${Args}}::out) is nondet :- (${pred}(${Args}),RelHandle0 = RelHandleIn), L),"
ArgCount=${ArgPos}
ArgPos=0
while test $ArgPos -lt $ArgCount; do
ArgSubPos=0
Pattern=''
delim=''
while test $ArgSubPos -lt $ArgCount; do
if test $ArgSubPos = $ArgPos; then
Pattern="${Pattern}${delim}${ArgAry[$ArgPos]}"
else
Pattern="${Pattern}${delim}_"
fi
ArgSubPos=$(($ArgSubPos + 1))
delim=","
done
if test $ArgPos = 0; then
ArgMapIn=ArgMapIn
else
ArgMapIn=ArgMap$(($ArgPos - 1))
fi
if test $(($ArgPos + 1)) = $ArgCount; then
ArgMapOut=ArgMapOut
else
ArgMapOut=ArgMap${ArgPos}
fi
if test ${Types[$ArgPos]} = mrs_rel_handle; then
echo " list.foldl(pred({${Pattern}}::in,AIn::in,AOut::out) is det :- "
echo " ((if multi_map.contains(RelMap,${Vars[$ArgPos]}) then"
echo " TL${ArgPos} = multi_map.lookup(RelMap,${Vars[$ArgPos]}),"
echo " RL${ArgPos} = list.map(func({RelHandleA,_,_,_}) = Ret :- Ret = RelHandleA,TL${ArgPos})"
echo " else"
echo " RL${ArgPos} = [${Vars[$ArgPos]}]),"
echo " list.foldl(pred(RelHandleB::in,AIn0::in,AOut0::out) is det :- AOut0 = multi_map.add(AIn0,wrap_rel_handle(${Vars[$ArgPos]}),RelHandleB),RL${ArgPos},AIn,AOut)),"
echo -n " L,${ArgMapIn},${ArgMapOut})"
else
echo -n " list.foldl(pred({${Pattern}}::in,AIn::in,AOut::out) is det :- AOut = multi_map.add(AIn,${WrapExprs[$ArgPos]},RelHandleIn),L,${ArgMapIn},${ArgMapOut})"
fi
ArgPos=$(($ArgPos + 1))
if test $ArgPos -lt $ArgCount; then
echo ,
else
echo
fi
done
lineDelim="else "
done
cat << EOF
else
impure unsafe_perform_io(print(Pred)),
error("unknown predicate").
EOF