1212import org .kframework .definition .Sentence ;
1313import org .kframework .kompile .KompileOptions ;
1414import org .kframework .frontend .FindK ;
15+ import org .kframework .frontend .KORE ;
16+ import org .kframework .frontend .Sort ;
1517import org .kframework .frontend .VisitK ;
1618import org .kframework .frontend .K ;
1719import org .kframework .frontend .KApply ;
1820import org .kframework .frontend .KLabel ;
1921import org .kframework .frontend .KRewrite ;
2022import org .kframework .frontend .KVariable ;
2123import org .kframework .utils .errorsystem .KEMException ;
24+ import scala .Option ;
25+ import scala .Tuple2 ;
26+ import scala .collection .Seq ;
2227
2328import java .util .ArrayList ;
2429import java .util .HashSet ;
@@ -61,7 +66,8 @@ public Module resolve(Module input) {
6166
6267 private Set <KLabel > klabels ;
6368
64- private KLabel getUniqueFreezerLabel (Module input , String nameHint ) {
69+ private Tuple2 <KLabel , Tuple2 <Seq <Sort >, Sort >> getUniqueFreezerLabel (Module input , String name , Integer pos ) {
70+ String nameHint = pos == null ? name : name + pos ;
6571 if (klabels .isEmpty ()) {
6672 klabels .addAll (mutable (input .definedKLabels ()));
6773 }
@@ -71,7 +77,19 @@ private KLabel getUniqueFreezerLabel(Module input, String nameHint) {
7177 freezer = KLabel ("#freezer" + nameHint + (counter ++ == 0 ? "" : counter ));
7278 } while (klabels .contains (freezer ));
7379 klabels .add (freezer );
74- return freezer ;
80+
81+ Tuple2 <Seq <Sort >, Sort > sig ;
82+ if (kompileOptions .minikore ) {
83+ Option <scala .collection .Set <Tuple2 <Seq <Sort >, Sort >>> sigSetOpt = input .signatureFor ().get (KLabel (name ));
84+ if (sigSetOpt .isEmpty ()) assert false ;
85+ scala .collection .Set <Tuple2 <Seq <Sort >, Sort >> sigSet = sigSetOpt .get ();
86+ if (sigSet .size () != 1 ) assert false ;
87+ sig = sigSet .iterator ().next ();
88+ } else {
89+ sig = null ;
90+ }
91+
92+ return Tuple2 .apply (freezer , sig );
7593 }
7694
7795 private Stream <? extends Sentence > resolve (Context context , Module input ) {
@@ -83,6 +101,9 @@ private Stream<? extends Sentence> resolve(Context context, Module input) {
83101
84102 int currentHolePosition [] = new int [] { 0 };
85103 int finalHolePosition [] = new int [] { 0 };
104+ int currentPosition [] = new int [] { 0 };
105+ int firstHolePosition [] = new int [] { -1 };
106+ final ArrayList <Integer > nonHolePositions = new ArrayList <>();
86107 // Find a heated hole
87108 // e.g., context ++(HOLE => lvalue(HOLE))
88109 K heated = new VisitK () {
@@ -108,10 +129,13 @@ public void apply(KVariable k) {
108129 if (!k .name ().equals ("HOLE" )) {
109130 vars .put (k , k );
110131 finalHolePosition [0 ] = currentHolePosition [0 ];
132+ nonHolePositions .add (currentPosition [0 ]);
111133 } else {
112134 holeVar = k ;
113135 currentHolePosition [0 ]++;
136+ if (firstHolePosition [0 ] == -1 ) firstHolePosition [0 ] = currentPosition [0 ];
114137 }
138+ currentPosition [0 ]++;
115139 super .apply (k );
116140 }
117141
@@ -126,23 +150,35 @@ public void apply(KApply k) {
126150 K cooled = RewriteToTop .toLeft (body );
127151 // TODO(dwightguth): generate freezers better for pretty-printing purposes
128152 List <ProductionItem > items = new ArrayList <>();
129- KLabel freezerLabel ;
153+ Tuple2 <KLabel , Tuple2 <Seq <Sort >, Sort >> freezerLabelAndSig ;
154+ int pos = kompileOptions .minikore ? firstHolePosition [0 ] : finalHolePosition [0 ];
130155 if (cooled instanceof KApply ) {
131- freezerLabel = getUniqueFreezerLabel (input , ((KApply )cooled ).klabel ().name () + finalHolePosition [ 0 ] );
156+ freezerLabelAndSig = getUniqueFreezerLabel (input , ((KApply )cooled ).klabel ().name (), pos );
132157 } else {
133- freezerLabel = getUniqueFreezerLabel (input , "" );
158+ freezerLabelAndSig = getUniqueFreezerLabel (input , "" , null );
134159 }
160+ KLabel freezerLabel = freezerLabelAndSig ._1 ();
161+ Tuple2 <Seq <Sort >, Sort > freezerLabelSig = freezerLabelAndSig ._2 ();
135162 items .add (Terminal (freezerLabel .name ()));
136163 items .add (Terminal ("(" ));
137- for (int i = 0 ; i < vars .size (); i ++) {
138- items .add (NonTerminal (Sort ("K" )));
139- items .add (Terminal ("," ));
164+ if (kompileOptions .minikore ) {
165+ for (int i : nonHolePositions ) {
166+ items .add (NonTerminal (freezerLabelSig ._1 ().apply (i )));
167+ items .add (Terminal ("," ));
168+ }
169+ assert (nonHolePositions .size () == vars .size ());
170+ } else {
171+ for (int i = 0 ; i < vars .size (); i ++) {
172+ items .add (NonTerminal (Sort ("K" )));
173+ items .add (Terminal ("," ));
174+ }
140175 }
141176 if (vars .size () > 0 ) {
142177 items .remove (items .size () - 1 );
143178 }
144179 items .add (Terminal (")" ));
145- Production freezer = Production (freezerLabel .name (), Sorts .KItem (), immutable (items ), Att ());
180+ Sort targetSort = kompileOptions .minikore ? freezerLabelSig ._2 () : Sorts .KItem ();
181+ Production freezer = Production (freezerLabel .name (), targetSort , immutable (items ), Att ());
146182 K frozen = KApply (freezerLabel , vars .values ().stream ().collect (Collections .toList ()));
147183 return Stream .of (freezer ,
148184 Rule (KRewrite (cooled , KSequence (heated , frozen )), requiresHeat , BooleanUtils .TRUE , context .att ().add ("heat" )),
0 commit comments