@@ -695,25 +695,57 @@ public static boolean allSymbolTerms(ASTTerm[] trees)
695695 }
696696
697697 public static boolean allNestedSymbolTerms (ASTTerm [] trees )
698- { if (trees .length == 0 )
698+ { // All either symbols or nested symbols
699+
700+ if (trees .length == 0 )
699701 { return false ; }
702+
700703 for (int i = 0 ; i < trees .length ; i ++)
701704 { ASTTerm tx = trees [i ];
702705 if (tx == null )
703706 { return false ; }
704- if (tx .arity () == 1 )
705- { ASTTerm ttx = tx .getTerm (0 );
706707
707- if (ttx instanceof ASTSymbolTerm ) { }
708- else
709- { return false ; }
708+ if (tx . arity () <= 1 && tx . isNestedSymbolTerm ())
709+ {
710+ System . out . println ( ">>> Nested symbol term: " + tx );
710711 }
711712 else
712713 { return false ; }
713714 }
714715 return true ;
715716 }
716717
718+ public abstract boolean isNestedSymbolTerm ();
719+
720+ public static boolean recursivelyNestedEqual (
721+ ASTTerm [] strees , ASTTerm [] ttrees )
722+ { // Each strees[i] is a symbol, literally equal
723+ // to the targets
724+
725+ if (strees .length == 0 )
726+ { return false ; }
727+
728+ if (strees .length != ttrees .length )
729+ { return false ; }
730+
731+ for (int i = 0 ; i < strees .length ; i ++)
732+ { ASTTerm sx = strees [i ];
733+ ASTTerm tx = ttrees [i ];
734+
735+ if (sx == null || tx == null )
736+ { return false ; }
737+
738+ String slit = sx .literalForm ();
739+ String tlit = tx .literalForm ();
740+
741+ if (slit .equals (tlit )) { }
742+ else
743+ { return false ; }
744+ }
745+
746+ return true ;
747+ }
748+
717749
718750 public static boolean functionalSymbolMapping (ASTTerm [] strees , ASTTerm [] ttrees )
719751 { // The correspondence is functional.
@@ -1110,6 +1142,121 @@ else if (xx.equals(yvect)) { }
11101142 return res ;
11111143 }
11121144
1145+ public static Vector createGeneralisedMappings
1146+ (ASTTerm [] xs , Expression trg )
1147+ { // Each xs[i] maps to same target.
1148+ // Create a separate mapping for each different arity
1149+ // of xs. Extract constant symbol values.
1150+
1151+ Vector res = new Vector ();
1152+ java .util .Map aritymap = new java .util .HashMap ();
1153+ Vector doms = new Vector ();
1154+
1155+ if (xs .length > 1 )
1156+ { for (int i = 0 ; i < xs .length ; i ++)
1157+ { ASTTerm xx = xs [i ];
1158+
1159+ if (xx != null )
1160+ { int n = xx .arity ();
1161+ Integer nx = new Integer (n );
1162+ doms .add (nx );
1163+ Vector aritynterms =
1164+ (Vector ) aritymap .get (nx );
1165+ if (aritynterms == null )
1166+ { aritynterms = new Vector ();
1167+ aritynterms .add (xx );
1168+ aritymap .put (nx ,aritynterms );
1169+ }
1170+ else
1171+ { aritynterms .add (xx ); }
1172+ }
1173+ }
1174+ }
1175+ else
1176+ { return res ; }
1177+
1178+ for (int j = 0 ; j < doms .size (); j ++)
1179+ { // For each arity set aritymap(doms(j))
1180+ // identify the generalised form of the terms
1181+
1182+ Integer nx = (Integer ) doms .get (j );
1183+ Vector arityns = (Vector ) aritymap .get (nx );
1184+
1185+ System .out .println (">>> Arity " + nx + " source terms are: " + arityns );
1186+ System .out .println ();
1187+
1188+ if (arityns .size () == 1 )
1189+ { ASTTerm st = (ASTTerm ) arityns .get (0 );
1190+ BasicExpression expr =
1191+ BasicExpression .newASTBasicExpression (st );
1192+ AttributeMatching am =
1193+ new AttributeMatching (expr ,trg );
1194+ res .add (am );
1195+ }
1196+ else
1197+ { ASTTerm st0 = (ASTTerm ) arityns .get (0 );
1198+ BasicExpression expr =
1199+ BasicExpression .newASTBasicExpression (st0 );
1200+ int n = nx .intValue ();
1201+ for (int p = 0 ; p < n ; p ++)
1202+ { if (ASTTerm .constantTerms (arityns ,p ))
1203+ { ASTTerm pterm = st0 .getTerm (p );
1204+ String st0p = pterm .literalForm ();
1205+ Expression exprp =
1206+ new BasicExpression (st0p );
1207+ expr .setParameter (p +1 , exprp );
1208+ }
1209+ else
1210+ { expr .setParameter (p +1 ,
1211+ new BasicExpression ("_" + (p +1 )));
1212+ }
1213+ }
1214+ AttributeMatching am =
1215+ new AttributeMatching (expr ,trg );
1216+ res .add (am );
1217+ }
1218+ }
1219+
1220+ System .out .println (">>> Generalised matchings: " + res );
1221+
1222+ return res ;
1223+ }
1224+
1225+ public static boolean constantTerms (Vector trms , int p )
1226+ { // The p subterms of all trms are the same
1227+
1228+ if (trms .size () == 0 )
1229+ { return false ; }
1230+
1231+ if (trms .size () == 1 )
1232+ { return true ; }
1233+
1234+ ASTTerm t0 = (ASTTerm ) trms .get (0 );
1235+ if (t0 .arity () <= p )
1236+ { return false ; }
1237+
1238+ ASTTerm subtermp = t0 .getTerm (p );
1239+
1240+ String lit = subtermp .literalForm ();
1241+
1242+ for (int i = 1 ; i < trms .size (); i ++)
1243+ { ASTTerm t = (ASTTerm ) trms .get (i );
1244+ ASTTerm subterm = t .getTerm (p );
1245+
1246+ // System.out.println(" " + p + " subterm of " + t + " is " + subterm + " =? " + lit);
1247+
1248+ if (subterm != null &&
1249+ subterm .literalForm ().equals (lit ))
1250+ { }
1251+ else
1252+ { return false ; }
1253+ }
1254+
1255+ // System.out.println(" " + p + " subterm is constant");
1256+
1257+ return true ;
1258+ }
1259+
11131260 /* public static boolean matchingTrees(ASTTerm[] xs, ASTTerm[] ys, ModelSpecification mod)
11141261 { // Is each ys[i] = xs[i], or corresponding under mod?
11151262
0 commit comments