File: semirel.gi

package info (click to toggle)
gap 4r7p5-2
  • links: PTS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 29,272 kB
  • ctags: 7,129
  • sloc: ansic: 107,802; xml: 46,868; sh: 3,548; perl: 2,329; makefile: 740; python: 94; asm: 62; awk: 6
file content (1362 lines) | stat: -rw-r--r-- 40,445 bytes parent folder | download
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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
#############################################################################
#W  semirel.gi                  GAP library                James D. Mitchell
##
##
#Y  Copyright (C)  1997,  Lehrstuhl D für Mathematik,  RWTH Aachen,  Germany
#Y  (C) 1998 School Math and Comp. Sci., University of St Andrews, Scotland
#Y  Copyright (C) 2002 The GAP Group
##
##  This file contains the declarations for equivalence relations on
##  semigroups. Of particular interest are Green's relations,
##  congruences, and Rees congruences.
##

#######################
#######################
##
#M GreensRRelation(<semigroup>)
#M GreensLRelation(<semigroup>)
#M GreensHRelation(<semigroup>)
#M GreensDRelation(<semigroup>)
#M GreensJRelation(<semigroup>)
##
## returns the appropriate equivalence relation which is stored as an attribute.
## The relation knows nothing about itself except its source, range, and what
## type of congruence it is.


InstallMethod(GreensRRelation, "for a semigroup", true, [IsSemigroup], 0,
function(X)
    local fam, rel, sc;

    fam :=  GeneralMappingsFamily( ElementsFamily(FamilyObj(X)),
	     ElementsFamily(FamilyObj(X)) );

    # Create the default type for the elements.
    rel :=  Objectify(NewType(fam,
	       IsEquivalenceRelation and IsEquivalenceRelationDefaultRep
	       and IsGreensRRelation), rec());

    SetSource(rel, X);
    SetRange(rel, X);
    SetIsLeftSemigroupCongruence(rel,true);
    
    if HasIsFinite(X) and IsFinite(X) then
       SetIsFiniteSemigroupGreensRelation(rel, true);
    fi;

    return rel;
end);

InstallMethod(GreensLRelation, "for a semigroup", true, [IsSemigroup], 0,
function(X)
    local fam, rel;

    fam :=  GeneralMappingsFamily( ElementsFamily(FamilyObj(X)),
	    ElementsFamily(FamilyObj(X)) );

    # Create the default type for the elements.
    rel :=  Objectify(NewType(fam,
	    IsEquivalenceRelation and IsEquivalenceRelationDefaultRep
	    and IsGreensLRelation), rec());

    SetSource(rel, X);
    SetRange(rel, X);
    SetIsRightSemigroupCongruence(rel,true);
    if HasIsFinite(X) and IsFinite(X) then
      SetIsFiniteSemigroupGreensRelation(rel, true);
    fi;

    return rel;
end);

InstallMethod(GreensJRelation, "for a semigroup", true, [IsSemigroup], 0,
function(X)
    local fam, rel;

    fam :=  GeneralMappingsFamily( ElementsFamily(FamilyObj(X)),
	    ElementsFamily(FamilyObj(X)) );

    # Create the default type for the elements.
    rel :=  Objectify(NewType(fam,
	    IsEquivalenceRelation and IsEquivalenceRelationDefaultRep
	    and IsGreensJRelation), rec());

    SetSource(rel, X);
    SetRange(rel, X);
    if HasIsFinite(X) and IsFinite(X) then
      SetIsFiniteSemigroupGreensRelation(rel, true);
    fi;

    return rel;
end);

InstallMethod(GreensDRelation, "for a semigroup", true, [IsSemigroup], 0,
function(X)
    local fam, rel;

    fam :=  GeneralMappingsFamily( ElementsFamily(FamilyObj(X)),
	    ElementsFamily(FamilyObj(X)) );

    # Create the default type for the elements.
    rel :=  Objectify(NewType(fam,
	    IsEquivalenceRelation and IsEquivalenceRelationDefaultRep
	    and IsGreensDRelation), rec());

    SetSource(rel, X);
    SetRange(rel, X);

    if HasIsFinite(X) and IsFinite(X) then
      SetIsFiniteSemigroupGreensRelation(rel, true);
    fi;

    return rel;
end);

InstallMethod(GreensHRelation, "for a semigroup", true, [IsSemigroup], 0,
function(X)
    local fam, rel;

    fam :=  GeneralMappingsFamily( ElementsFamily(FamilyObj(X)),
	    ElementsFamily(FamilyObj(X)) );

    # Create the default type for the elements.
    rel :=  Objectify(NewType(fam,
	    IsEquivalenceRelation and IsEquivalenceRelationDefaultRep
	    and IsGreensHRelation), rec());

    SetSource(rel, X);
    SetRange(rel, X);

    if HasIsFinite(X) and IsFinite(X) then
      SetIsFiniteSemigroupGreensRelation(rel, true);
    fi;

    return rel;
end);

InstallMethod( ViewObj, "for GreensJRelation", [IsGreensJRelation],
    function( obj )
    Print( "< Green's J-relation on ");
    ViewObj(Source(obj));
    Print(" >");
end );

InstallMethod( ViewObj, "for GreensDRelation", [IsGreensDRelation],
    function( obj )
    Print( "< Green's D-relation on ");
    ViewObj(Source(obj));
    Print(" >");
end );

InstallMethod( ViewObj, "for GreensHRelation", [IsGreensHRelation],
    function( obj )
    Print( "< Green's H-relation on ");
    ViewObj(Source(obj));
    Print(" >");
end );

InstallMethod(\=, "for GreensRelation", [IsGreensRelation, IsGreensRelation],
    function(rel1, rel2)
    if not Source(rel1)=Source(rel2) then
      Error("Green's relations do not belong to the same semigroup");
    elif IsGreensRRelation(rel1) and not IsGreensRRelation(rel2) then
      return false;
    elif IsGreensLRelation(rel1) and not IsGreensLRelation(rel2) then
      return false;
    elif IsGreensHRelation(rel1) and not IsGreensHRelation(rel2) then
      return false;
    elif IsGreensDRelation(rel1) and not IsGreensDRelation(rel2) then
      return false;
    elif IsGreensJRelation(rel1) and not IsGreensJRelation(rel2) then
      return false;
    else
      return true;
    fi;
end);

#############################################################################
##
##  The following operations are constructors for Green's class with
##  a given element as a representative. The call is for semigroups
##  and an element in the semigroup. This function doesn't check that
##  the element is actually a member of the semigroup.
##
#O  GreensRClassOfElement(<semigroup>, <representative>)
#O  GreensLClassOfElement(<semigroup>, <representative>)
#O  GreensJClassOfElement(<semigroup>, <representative>)
#O  GreensDClassOfElement(<semigroup>, <representative>)
#O  GreensHClassOfElement(<semigroup>, <representative>)
##

InstallMethod(GreensRClassOfElement, "for a semigroup and object",
[IsSemigroup and HasIsFinite and IsFinite, IsObject],
function(s,e)
  return EquivalenceClassOfElementNC( GreensRRelation(s), e );
end);

InstallMethod(GreensLClassOfElement, "for a semigroup and object",
[IsSemigroup and HasIsFinite and IsFinite, IsObject],
function(s,e)
  return EquivalenceClassOfElementNC( GreensLRelation(s), e );
end);


InstallMethod(GreensHClassOfElement, "for a semigroup and object",
[IsSemigroup and HasIsFinite and IsFinite, IsObject],
function(s,e)
  return EquivalenceClassOfElementNC( GreensHRelation(s), e );
end);


InstallMethod(GreensDClassOfElement, "for a semigroup and object", 
[IsSemigroup and HasIsFinite and IsFinite, IsObject],
function(s,e)
  return EquivalenceClassOfElementNC( GreensDRelation(s), e );
end);

InstallMethod(GreensJClassOfElement, "for a semigroup and object", 
[IsSemigroup and HasIsFinite and IsFinite, IsObject],
function(s,e)
  return EquivalenceClassOfElementNC( GreensJRelation(s), e );
end);

#

InstallMethod(CanonicalGreensClass, "for a Green's class", 
[IsGreensClass],
function(class)
  local x, canon;

  if IsGreensRClass(class) then
    x:=GreensRClasses(ParentAttr(class));
    canon:=First(x, y-> Representative(class) in y);
    SetCanonicalGreensClass(class, canon);
  elif IsGreensLClass(class) then
     x:=GreensLClasses(ParentAttr(class));
    canon:=First(x, y-> Representative(class) in y);
    SetCanonicalGreensClass(class, canon);
  elif IsGreensHClass(class) then
     x:=GreensHClasses(ParentAttr(class));
    canon:=First(x, y-> Representative(class) in y);
    SetCanonicalGreensClass(class, canon);
  elif IsGreensDClass(class) then
     x:=GreensDClasses(ParentAttr(class));
    canon:=First(x, y-> Representative(class) in y);
    SetCanonicalGreensClass(class, canon);
  elif IsGreensJClass(class) then
     x:=GreensJClasses(ParentAttr(class));
    canon:=First(x, y-> Representative(class) in y);
    SetCanonicalGreensClass(class, canon);
  fi;

  return canon;
end);

#################
#################
##
#M ImagesElm(<grelation>, <elm>)
##
## method to find the images under a GreensRelation of an
## element of a semigroup.
##

InstallMethod(ImagesElm, "for a Green's equivalence", true, [IsGreensRelation, IsObject], 0,
    function(rel, elm)
    local exp, semi;

    semi:=Source(rel);

    if IsGreensRRelation(rel) then
	    exp:=GreensRClassOfElement(semi, elm);
    elif IsGreensLRelation(rel) then
	    exp:=GreensLClassOfElement(semi, elm);
    elif IsGreensHRelation(rel) then
	    exp:=GreensHClassOfElement(semi, elm);
    elif IsGreensDRelation(rel) then
	    exp:=GreensDClassOfElement(semi, elm);
    elif IsGreensJRelation(rel) then
	    exp:=GreensJClassOfElement(semi, elm);
    fi;

    return AsSSortedList(exp);
 end);

#################
#################
##
#M Successors(<grelation>)
##
## returns ImagesElm for one element in each class of <grelation>
##

InstallMethod(Successors, "for a Green's equivalence", true, [IsGreensRelation], 0,
    function( rel )
    return List(EquivalenceClasses(rel), AsSSortedList);
end);

#################
#################
##
#M AsSSortedList(<gclass>)
##
## returns the elements of the Greens class <gclass>
##

InstallMethod(AsSSortedList, "for a Green's class", true, [IsGreensClass], 0,
	x-> AsSSortedList(CanonicalGreensClass(x)));

#################
#################
##
#M \= (<class1>,<class2>)
##

InstallMethod(\=, "for Green's classes",  true, [IsGreensClass, IsGreensClass],
0,
function(class1,class2)
  if not ParentAttr(class1)=ParentAttr(class2) then
    Error("Green's classes do not belong to the same semigroup");
  elif not EquivalenceClassRelation(class1)=EquivalenceClassRelation(class2) then
    Error("Green's classes are not of the same type");
  else
    return Representative(class1) in class2;
  fi;
end);

#################
#################
##
#M Size(<gclass>)
##
## size of a Greens class
##

InstallMethod(Size, "for Green's classes", true, [IsGreensClass], 0,
function(class)
   return Size(Elements(class));
end);

#################
#################
##
#M <elm> in <gclass>
##
## membership test for a Greens class
##

InstallMethod(\in, "membership test of Green's class", true, [IsObject, IsGreensClass], 0,
function(elm, class)
  if elm=Representative(class) then
     return true;
  fi;
  return elm in Elements(class);
end);

#################
#################
##
#M EquivalenceRelationPartition(<grelation>)
##
##

InstallMethod(EquivalenceRelationPartition, "for a Green's equivalence", true, [IsEquivalenceRelation and IsGreensRelation], 0,
function(rel)
  return Filtered(Successors(rel), x-> not Length(x)=1);
end);

#################
#################
##
#M EquivalenceClassOfElementNC(<grelation>, <elt>)
##
## new methods required so that what is returned by this function
## is the appropriate type of Green's class

#JDM this should be 5 methods

InstallOtherMethod(EquivalenceClassOfElementNC, 
"for a Green's relation and object", 
[IsEquivalenceRelation and IsGreensRelation, IsObject],
function(rel, rep)
  local filts, new;
  
  filts:=IsEquivalenceClass and IsEquivalenceClassDefaultRep;
  if IsGreensRRelation(rel) then
    filts:=filts and IsGreensRClass;
  elif IsGreensLRelation(rel) then
    filts:=filts and IsGreensLClass;
  elif IsGreensHRelation(rel) then
    filts:=filts and IsGreensHClass;
  elif IsGreensDRelation(rel) then
    filts:=filts and IsGreensDClass;
  elif IsGreensJRelation(rel) then
    filts:=filts and IsGreensJClass;
  fi;
  
  new:= Objectify(NewType(CollectionsFamily(FamilyObj(rep)), filts), rec());

  SetEquivalenceClassRelation(new, rel);
  SetRepresentative(new, rep);
  SetParent(new, UnderlyingDomainOfBinaryRelation(rel));

  return new;
end);

#################
#################
##
#M EquivalenceClasses(<grelation>)
##
##

InstallMethod(EquivalenceClasses, "for a Green's R-relation", true, [IsEquivalenceRelation and IsGreensRRelation], 0,
x->GreensRClasses(Source(x)));

InstallMethod(EquivalenceClasses, "for a Green's L-relation", true, [IsEquivalenceRelation and IsGreensLRelation], 0,
x->GreensLClasses(Source(x)));

InstallMethod(EquivalenceClasses, "for a Green's H-relation", true, [IsEquivalenceRelation and IsGreensHRelation], 0,
x->GreensHClasses(Source(x)));

InstallMethod(EquivalenceClasses, "for a Green's D-relation", true, [IsEquivalenceRelation and IsGreensDRelation], 0,
x->GreensDClasses(Source(x)));

InstallMethod(EquivalenceClasses, "for a Green's J-relation", true, [IsEquivalenceRelation and IsGreensJRelation], 0,
x->GreensJClasses(Source(x)));

#################
#################
##
#M  RClassOfHClass(<hclass>)
#M  LClassOfHClass(<hclass>)
#M  DClassOfHClass(<hclass>)
#M  DClassOfLClass(<lclass>)
#M  DClassOfRClass(<rclass>)
##
##  returns the XClass containing <hclass>, <lclass>, or <rclass>

InstallMethod(RClassOfHClass, "for a Green's H-class", true, [IsGreensHClass], 0,
    function(hc)
    local x;
    x:=GreensRClasses(ParentAttr(hc));
    return First(x, y-> Representative(hc) in y);
end);

InstallMethod(LClassOfHClass, "for a Green's H-class", true, [IsGreensHClass], 0,
  function(hc)
       local x;
    x:=GreensLClasses(ParentAttr(hc));
    return First(x, y-> Representative(hc) in y);
end);

InstallMethod(DClassOfHClass, "for a Green's H-class", true, [IsGreensHClass], 0,
 x-> DClassOfHClass(CanonicalGreensClass(x)));

InstallMethod(DClassOfLClass, "for a Green's L-class", true, [IsGreensLClass], 0,
 x-> DClassOfLClass(CanonicalGreensClass(x)));

InstallMethod(DClassOfRClass, "for a Green's R-class", true, [IsGreensRClass], 0,
 x-> DClassOfRClass(CanonicalGreensClass(x)));

#################
#################
##
#M  GreensRClasses(<semigroup>)
#M  GreensLClasses(<semigroup>)
#M  GreensJClasses(<semigroup>)
#M  GreensDClasses(<semigroup>)
#M  GreensHClasses(<semigroup>)
##
##  find all the classes of a particular type

InstallMethod(GreensRClasses, "for a semigroup", true, [IsSemigroup], 0,
function( semi )
local rrel, sc, i, classes, x, rc;

  rrel:=GreensRRelation(semi);

   if not HasRightCayleyGraphSemigroup(semi) then
     FroidurePinExtendedAlg(semi);
   fi;

   sc:=STRONGLY_CONNECTED_COMPONENTS_DIGRAPH(RightCayleyGraphSemigroup(semi));

   #for faster calculation of Green's D-, J- and H-rels
   SetInternalRepGreensRelation(rrel, sc); classes:=[];

   for i in [1..Length(sc)] do
     rc:=GreensRClassOfElement(semi, Elements(semi)[sc[i][1]]);
     Add(classes, rc);
     SetAsSSortedList(classes[i], Elements(semi){sc[i]});
     SetSize(classes[i], Size(sc[i]));
   od;

   SetGreensRClasses(semi,classes);

   return classes;

end);

InstallOtherMethod(GreensRClasses, "for a Green's D-class", true, [IsGreensDClass], 0,
x-> GreensRClasses(CanonicalGreensClass(x)));

InstallOtherMethod(GreensLClasses, "for a semigroup", true, [IsSemigroup], 0,
   function( semi )
   local lrel, sc, i, classes, x, lc;

   lrel:=GreensLRelation(semi);

   if not HasLeftCayleyGraphSemigroup(semi) then
     FroidurePinExtendedAlg(semi);
   fi;

   sc:=STRONGLY_CONNECTED_COMPONENTS_DIGRAPH(LeftCayleyGraphSemigroup(semi));

   #for faster calculation of Green's D-,J- and H-rels
   SetInternalRepGreensRelation(lrel, sc); classes:=[];

   for i in [1..Length(sc)] do
     lc:=GreensLClassOfElement(semi,Elements(semi)[sc[i][1]]);
     Add(classes, lc);
     SetAsSSortedList(lc, Elements(semi){sc[i]});
     SetSize(lc, Size(sc[i]));
   od;

   SetGreensLClasses(semi,classes);

   return classes;

end);

InstallOtherMethod(GreensLClasses, "for a Green's D-class", true, [IsGreensDClass], 0,
x-> GreensLClasses(CanonicalGreensClass(x)));

InstallOtherMethod(GreensJClasses, "for a semigroup",
[IsSemigroup and IsFinite], GreensDClasses);

InstallMethod(GreensDClasses, "for a semigroup", true, [IsSemigroup], 0,
function(semi)
  local lrel, rrel, INT_L, INT_R, elts, INT_Rclasses, INT_Lclasses,
  INT_Dclasses, index, pos, INT_rc, INT_hc, INT_lc, new, newINT, Dclasses,
  Lclasses, Rclasses, Hclasses, LHclasses, RHclasses, i, j, positions, R, L; 

  ## compute the join of the R- and L-relations

  L:=GreensLClasses(semi); R:=GreensRClasses(semi);
  lrel:=GreensLRelation(semi); rrel:=GreensRRelation(semi);
  INT_L:=InternalRepGreensRelation(lrel);
  INT_R:=InternalRepGreensRelation(rrel);
  elts:=Elements(semi);

  #these are to collect the R and L-classes that comprise the D-class
  INT_Rclasses:=[]; INT_Lclasses:=[]; INT_Dclasses:=[];
  Dclasses:=[]; Lclasses:=[]; Rclasses:=[]; Hclasses:=[];
  RHclasses:=List(INT_R, x-> []); LHclasses:=List(INT_L, x->[]); positions:=[];

  index:=0;

  for i in [1..Length(INT_L)] do
    INT_lc:=INT_L[i];
    pos:=PositionProperty(INT_Dclasses, x->IsSubset(x, INT_lc));
    #JDM isn't it enough that INT_lc contains a single element in
    #JDM INT_Dclasses[something].
    if pos=fail then
      index:=index+1; Add(Rclasses, []); 
      Add(Hclasses, []); Add(positions, []);
      Add(INT_Rclasses, []);

      for j in [1..Length(INT_R)] do
        INT_rc:=INT_R[j];
        INT_hc:=Intersection(INT_rc, INT_lc);
        if INT_hc<>[] then
          new:=GreensHClassOfElement(semi, elts[INT_hc[1]]);
          SetAsSSortedList(new, elts{INT_hc});
          SetSize(new, Length(INT_hc));
          Add(Hclasses[index], new);
          Add(RHclasses[j], new); Add(LHclasses[i], new);

          Add(INT_Rclasses[index], INT_rc);

          new:=R[j];
          Add(Rclasses[index], new);
          Add(positions[index], j);

        fi;
      od;

      newINT:=Concatenation(INT_Rclasses[index]);
      Add(INT_Dclasses, newINT);

      new:=GreensDClassOfElement(semi, elts[newINT[1]]);
      SetAsSSortedList(new, elts{newINT});
      SetSize(new, Length(newINT));
      SetGreensRClasses(new, Rclasses[index]);
      Add(Dclasses, new);

      Add(INT_Lclasses, [INT_lc]);

      SetDClassOfLClass(L[i], Dclasses[index]);
      Add(Lclasses, [L[i]]);
    else
      Add(INT_Lclasses[pos], INT_lc);

      SetDClassOfLClass(L[i], Dclasses[pos]);
      Add(Lclasses[pos], L[i]);

      for j in [1..Length(INT_Rclasses[pos])] do
        INT_rc:=INT_Rclasses[pos][j];
        INT_hc:=Intersection(INT_rc, INT_lc);

        new:=GreensHClassOfElement(semi, elts[INT_hc[1]]);
        SetAsSSortedList(new, elts{INT_hc});
        SetSize(new, Length(INT_hc));
        Add(Hclasses[pos], new);
        SetLClassOfHClass(new, GreensLClasses(semi)[i]);
        Add(LHclasses[i], new);
        SetRClassOfHClass(new, GreensRClasses(semi)[positions[pos][j]]);
        Add(RHclasses[positions[pos][j]], new);
      od;
    fi;
  od;

  SetGreensDClasses(semi, Dclasses);
  SetGreensHClasses(semi, Concatenation(Hclasses));

  for i in [1..index] do
    for j in [1..Length(Rclasses[i])] do
      SetDClassOfRClass(Rclasses[i][j], Dclasses[i]);
    od;
    for j in [1..Length(Hclasses[i])] do
      SetDClassOfHClass(Hclasses[i][j], Dclasses[i]);
    od;
    SetGreensLClasses(Dclasses[i], Lclasses[i]);
    SetGreensHClasses(Dclasses[i], Hclasses[i]);
  od;

  for i in [1..Length(INT_R)] do
    SetGreensHClasses(GreensRClasses(semi)[i],  RHclasses[i]);
  od;
  for i in [1..Length(INT_L)] do
    SetGreensHClasses(GreensLClasses(semi)[i],  LHclasses[i]);
  od;

   return Dclasses;
end);

InstallMethod(GreensHClasses, "for a semigroup", true, [IsSemigroup], 0,
function(semi)
  GreensDClasses(semi);
  return GreensHClasses(semi);
end);

InstallOtherMethod(GreensHClasses, "for a Green's Class", true, [IsGreensDClass], 0,
x-> GreensHClasses(CanonicalGreensClass(x)));

InstallOtherMethod(GreensHClasses, "for a Green's Class", true, [IsGreensRClass], 0,
x-> GreensHClasses(CanonicalGreensClass(x)));

InstallOtherMethod(GreensHClasses, "for a Green's Class", true, [IsGreensLClass], 0,
x-> GreensHClasses(CanonicalGreensClass(x)));

#############################################################################
##
#O  IsRegularDClass(<greens class>)
##
##  returns true if the class contains an idempotent
##

InstallMethod(IsRegularDClass, "for a Green's D class", true,
        [IsGreensDClass],0,
        x-> ForAny(GreensRClassOfElement(ParentAttr(x), Representative(x)),
		    IsIdempotent));

InstallMethod(IsGreensLessThanOrEqual, "for two Green's classes",
[IsGreensClass, IsGreensClass],
function(gcL,gcR)
  local a,b;

  a := Representative(gcL);
  b := Representative(gcR);

  if IsGreensRClass(gcL) and IsGreensRClass(gcR) then
    return a in RightMagmaIdealByGenerators(ParentAttr(gcR),[b]);
  elif IsGreensLClass(gcL) and IsGreensLClass(gcR) then
    return a in LeftMagmaIdealByGenerators(ParentAttr(gcR),[b]);
  elif (IsGreensJClass(gcL) and IsGreensJClass(gcR)) or
    (IsGreensDClass(gcL) and IsGreensDClass(gcR) and
      IsFinite(ParentAttr(gcL))) then
    return a in MagmaIdealByGenerators(ParentAttr(gcR),[b]);
  fi;

  Error("Green's classes are not of the same type or not L-, R-, or J-classes");
  return;
end);

#############################################################################
##
#M  IsGroupHClass( <H> )
##
##  returns true if the Greens H-class <H> is a group, which in turn is
##  true if and only if <H>^2 intersects <H>.
##
InstallMethod(IsGroupHClass, "for Green's H-class", true,
    [IsGreensHClass], 0, h->ForAny(h, IsIdempotent));

############################################################################
##
#M  GroupHClassOfGreensDClass( <Dclass> )
##
##  for a D class <Dclass> of a semigroup,
##  returns a group H class of the D class, or `fail' if there is no
##  group H class.
##
## (if d contains an idempotent, then it is regular, and so contains
##  at least one idempotent in *each* R-class.)

InstallMethod(GroupHClassOfGreensDClass, "for a Green's H-class", true,
    [IsGreensDClass], 0,

    function(d)
        local idm, rcs;

        rcs:=GreensRClasses(d);

        idm := First(rcs[1], IsIdempotent);
        if idm=fail then
          return fail;
        else
          return GreensHClassOfElement(ParentAttr(d),idm);
        fi;
    end);

#############################################################################
##
#A  EggBoxOfDClass( <D> )
##
## this returns a matrix with the j-th entry in the i-th row
## being the intersection of the i-th R-class and the j-th L-class
##Ê(by the construction of GreensHClasses)
##

InstallMethod(EggBoxOfDClass, "for a Green's D class", true,
        [IsGreensDClass],0,
    function(d)

    return List(GreensRClasses(d), x-> GreensHClasses(x));
end);

#############################################################################
##
#F  DisplayEggBoxOfDClass( <D> )
##
##  A "picture" of the D class <D>, as an array of 1s and 0s.
##  A 1 represents a group H class.
##

InstallGlobalFunction(DisplayEggBoxOfDClass,
    function(d)
        if not IsGreensDClass(d) then
            Error("requires IsGreensDClass");
        fi;

        PrintArray(
            List(EggBoxOfDClass(d), r->List(r,
                function(h)
                   if IsGroupHClass(h) then
                        return 1;
                   else
                        return 0;
                   fi;
                end))
       );
    end);

#############################################################################
##
#M  DisplayEggBoxesOfSemigroup( <S> )
##

InstallMethod(DisplayEggBoxesOfSemigroup, "for finite semigroups",
    [IsTransformationSemigroup],
function(X)
local dclasses, layer, class, len, i, D;

   dclasses:=GreensDClasses(X);
   layer:=List([1..DegreeOfTransformationSemigroup(X)], x-> []);

   for class in dclasses do
     Add(layer[RankOfTransformation(Representative(class))], [class,
	Size(GreensHClasses(class)[1]), IsRegularDClass(class)]);
   od;

   len:= Length(layer);
   for i in [len, len-1..1] do
        if layer[i] <> [] then
	    for D in layer[i] do
	  	Print("Rank ", i, ", H-class size ", D[2]);
		if D[3] then
                  Print(", regular \n");
 	        else
 		  Print(", non-regular \n");
                fi;

                DisplayEggBoxOfDClass(D[1]);

            od;
        fi;
   od;
end );

####################
####################
##
#M FroidurePinSimpleAlg(<semigroup>);
##
## for details of the workings of this algorithm see:
##
## V. Froidure, and J.-E. Pin, Algorithms for computing finite semigroups.
## Foundations of computational mathematics (Rio de Janeiro, 1997), 112-126,
## Springer, Berlin,  1997.
##
## this function returns [elements of <semigroup>, set of defining relations
## for <semigroup>, the fp semigroup isomorphic to <semigroup>]. This is only
## included because it may give a quicker way of finding a presentation for
## <semigroup> than the extended algorithm.

InstallMethod(FroidurePinSimpleAlg, "for a finite monoid", 
[IsMonoid and HasIsFinite and IsFinite and HasGeneratorsOfMonoid],
function(semi)
  local  gens, concreteelts, free, freegens, fpelts, rules, Last, upos, u, i,
   newelt, newword, j, new;

  gens:=GeneratorsOfMonoid(semi);
  concreteelts:=[One(semi)];

  free:=FreeMonoid(Size(gens));
  freegens:=GeneratorsOfMonoid(free);
  fpelts:=[One(free)];
  rules:=[];

  Last:=1;
  upos:=0;

  repeat
    upos:=upos+1;
    u:=concreteelts[upos];

    for i in [1..Length(gens)] do
      newelt:=u*gens[i];
      newword:=fpelts[upos]*freegens[i];

      j:=0;
      new:=true;

      repeat#hmmm.... JDM
        j:=j+1;
        if newelt=concreteelts[j] then
          Add(rules, [newword, fpelts[j]]);
          new:=false;
        fi;
      until j=Last or not new;

      if new then
        Add(concreteelts, newelt);
        Add(fpelts, newword);
        Last:=Last+1;
      fi;
    od;

  until upos=Last;

  return [concreteelts, rules, free/rules];

end);

####################
####################
##
#M  FroidurePinExtendedAlg(<semigroup>);
##
##  for details of the workings of this algorithm see:
##
##  V. Froidure, and J.-E. Pin, Algorithms for computing finite semigroups.
##  Foundations of computational mathematics (Rio de Janeiro, 1997), 112-126,
##  Springer, Berlin,  1997.
##
##  this function returns nothing, but determines the elements, size,
##  a presentation, and the left and right Cayley graphs of <semigroup>.
##
##  JDM shouldn't produce fp representation if input is fp semigroup!
##

InstallMethod(FroidurePinExtendedAlg, "for a finite semigroup",
[IsSemigroup], 
function(m)
  local gens, k, free, freegens, actualelts, fpelts, rules, i, u, v, Last,
        currentlength, b, s, r, newelt, j, p, new, length, newword, first,
        final, prefix, suffix, next, postmult, reducedflags, premult, fpsemi,
        old, sortedelts, pos, semi, perm, free2;

  if not IsFinite(m) then 
    return fail;
  fi;

  if not IsMonoid(m) then
      semi := MonoidByAdjoiningIdentity(m);
  else
      semi:=m;
  fi;

  #gens:=Set(GeneratorsOfMonoid(semi));
  gens:=Set(Filtered(GeneratorsOfMonoid(semi), x-> not IsOne(x)));
  k:=Length(gens);
  free:=FreeMonoid(k);
  freegens:=GeneratorsOfMonoid(free);
  actualelts:=Concatenation([One(semi)], gens);
  fpelts:=Concatenation([One(free)], freegens);
  sortedelts:=List(Concatenation([One(semi)], gens));

  #output

  Sort(sortedelts);
  rules:=[];
  pos:=List([1..k+1], x-> Position(actualelts, sortedelts[x]));

  # table containing all data
  # for a word <u>

  # position of first letter in <gens>
  first:=Concatenation([fail], [1..k]);
  # position of last letter in <gens>
  final:=Concatenation([fail], [1..k]);
  # position of prefix of length |u|-1 in <fpelts>
  prefix:=Concatenation([fail], List([1..k], x->1));
  # position of suffix of length |u|-1 in <fpelts>
  suffix:=Concatenation([fail], List([1..k], x->1));
  # position of u*freegens[i] in <fpelts>
  postmult:=Concatenation([[2..k+1]], List([1..k], x-> []));
  # true if u*freegens[i] is the same word as fpelts[i]
  reducedflags:=Concatenation([List([1..k], x->  true)], List([1..k], x-> []));
  # position of freegens[i]*u in <fpelts>
  premult:=Concatenation([[2..k+1]],  List([1..k], x-> []));
  # length of <u>
  length:=Concatenation([0], List([1..k], x->1));

  # initialize loop

  u:=2;               # position of the first generator
  v:=u;               # place holder
  Last:=k+1;          # the current position of the last element in <fpelts>
  currentlength:=1;   # current length of words under consideration

  # loop

  repeat

    while u<=Last and length[u]=currentlength do

      b:=first[u];
      s:=suffix[u];

      for i in [1..k] do #loop over generators

	newword:=fpelts[u]*freegens[i]; # newword=u*a_i

	if not reducedflags[s][i] then  # if s*a_i is not reduced
	  r:=postmult[s][i];            # r=s*a_i
	  if fpelts[r]=One(free) then   # r=1
	    postmult[u][i]:=b+1; 
            reducedflags[u][i]:=true;   # u*a_i=b and it is reduced
	  else
	    postmult[u][i]:=postmult[premult[prefix[r]][b]][final[r]];
            #\rho(u*a_i)=\rho(\rho(b*r)*l(r))
	    reducedflags[u][i]:=(newword=fpelts[postmult[u][i]]); 
            # if \rho(u*a_i)=u*a_i then true
	  fi;
        else

	  newelt:=actualelts[u]*gens[i];      # newelt=nu(u*a_i)
          old:=PositionSorted(sortedelts, newelt);
          if old<=Last and newelt=sortedelts[old] then
            old:=pos[old];
	    Add(rules, [newword, fpelts[old]]);
	    postmult[u][i]:=old; 
            reducedflags[u][i]:=false;  # u*a_i represents the same elt as
                                        # fpelts[j] and is (hence) not reduced
	  else
            Add(fpelts, newword); Add(first, b); Add(final, i);   
            # add all its info to the table
            Add(prefix,u); Add(suffix, postmult[suffix[u]][i]);   
            # u=b*suffix(u)*a_i
            Add(postmult, []); Add(reducedflags, []); Add(premult, []);
            Add(length, length[u]+1); Add(actualelts, newelt);

	    Last:=Last+1;
	    postmult[u][i]:=Last; reducedflags[u][i]:=true;   
            # the word u*a_i is a new elt
            # and is hence reduced

            AddSet(sortedelts, newelt);

            CopyListEntries( pos, old, 1, pos, old+1, 1, Last-old );
            pos[old] := Last;

	  fi;
       fi;
     od;

      u:=u+1;

    od;
    u:=v;  # go back to the first elt with length=currentlength

    while u<=Last and length[u]=currentlength do
      p:=prefix[u];
      for i in [1..k] do
	premult[u][i]:=postmult[premult[p][i]][final[u]];
        # \rho(a_i*u)=\rho(\rho(a_i*p)*final(u))
      od;
      u:=u+1;
    od;

    v:=u;

    currentlength:=currentlength+1;

  until u=Last+1;

  if IsMonoid(m) then

    fpsemi:=free/rules;
    fpelts:=List(fpelts, function(x)
      local new;
      new:=MappedWord(x, FreeGeneratorsOfFpMonoid(fpsemi),
       GeneratorsOfMonoid(fpsemi));

      SetIsFpMonoidReducedElt(new, true);
      return new;
    end);

    SetAsSSortedList(fpsemi, fpelts);
    SetSize(fpsemi, Last);
    SetLeftCayleyGraphSemigroup(fpsemi, premult);
    SetRightCayleyGraphSemigroup(fpsemi, postmult);
    SetAssociatedConcreteSemigroup(fpsemi, semi);

    #JDM if KnuthBendixRewritingSystem was an attribute and not an operation
    #JDM it would be possible to set that it is confluent at this point

    perm:=PermListList(pos, [1..Last]);
    premult:=Permuted(OnTuplesTuples(premult, perm), perm);
    postmult:=Permuted(OnTuplesTuples(postmult, perm), perm);

    SetAsSSortedList(m, sortedelts);
    SetSize(m, Last);
    SetLeftCayleyGraphSemigroup(m, premult);
    SetRightCayleyGraphSemigroup(m, postmult);
    SetAssociatedFpSemigroup(m, fpsemi);

    u:=SemigroupHomomorphismByImagesNC(m, fpsemi,
         List(pos, x-> fpelts[x]));
    SetInverseGeneralMapping(u, SemigroupHomomorphismByImagesNC(fpsemi, m,
    actualelts));
    SetIsTotal(u, true); SetIsInjective(u, true);
    SetIsSurjective(u, true); SetIsSingleValued(u, true);
    SetIsomorphismFpMonoid(m, u);

  else

    #get rid of the identity! JDM better to do this online?

    free2:=FreeSemigroup(k);

    rules:=List(rules, x-> [MappedWord(x[1], GeneratorsOfMonoid(free),
     GeneratorsOfSemigroup(free2)), MappedWord(x[2], GeneratorsOfMonoid(free),
     GeneratorsOfSemigroup(free2))]);

    fpsemi:=free2/rules;

    fpelts:=List(fpelts{[2..Last]}, function(x)
      local new;
      new:=MappedWord(x, GeneratorsOfMonoid(free),
       GeneratorsOfSemigroup(fpsemi));
      SetIsFpSemigpReducedElt(new, true);
      return new;
    end);
    SetAsSSortedList(fpsemi, fpelts);
    SetSize(fpsemi, Last-1);

    premult:=premult{[2..Length(premult)]}-1;
    postmult:=postmult{[2..Length(postmult)]}-1;

    SetLeftCayleyGraphSemigroup(fpsemi, premult);
    SetRightCayleyGraphSemigroup(fpsemi, postmult);
    SetAssociatedConcreteSemigroup(fpsemi, m);

    sortedelts:=sortedelts{[2..Last]};
    actualelts:=actualelts{[2..Length(actualelts)]};
    pos:=pos{[2..Last]}-1;
    perm:=PermListList(pos, [1..Last-1]);
    sortedelts := List(sortedelts,
    UnderlyingSemigroupElementOfMonoidByAdjoiningIdentityElt);
    actualelts:=List(actualelts,
     UnderlyingSemigroupElementOfMonoidByAdjoiningIdentityElt); 

    premult:=Permuted(OnTuplesTuples(premult, perm), perm);
    postmult:=Permuted(OnTuplesTuples(postmult, perm), perm);

    SetAsSSortedList(m, sortedelts);
    SetSize(m, Last-1);
    SetLeftCayleyGraphSemigroup(m, premult);
    SetRightCayleyGraphSemigroup(m, postmult);
    SetAssociatedFpSemigroup(m, fpsemi);

    u:=SemigroupHomomorphismByImagesNC(m, fpsemi,
        List(pos, x-> fpelts[x]));

    SetInverseGeneralMapping(u, SemigroupHomomorphismByImagesNC(fpsemi, m,
    actualelts));

    SetIsTotal(u, true); SetIsInjective(u, true);
    SetIsSurjective(u, true); SetIsSingleValued(u, true);
    SetIsomorphismFpSemigroup(m, u);
  fi;

end);

#############################################################################
##
##        Free Semigroups
##
#############################################################################

#############################################################################
##
#M  GreensRRelation(<semigroup>)
#M  GreensLRelation(<semigroup>)
#M  GreensJRelation(<semigroup>)
#M  GreensDRelation(<semigroup>)
#M  GreensHRelation(<semigroup>)
##
##  Green's relations for free semigroups
##
##
InstallMethod(GreensRRelation, "for free semigroups", true,
        [IsSemigroup and IsFreeSemigroup], 0,
    function(s)
        Info(InfoWarning,1,
            "Green's relations for infinite semigroups is not supported");
        return fail;
    end);

InstallMethod(GreensLRelation, "for free semigroups", true,
        [IsSemigroup and IsFreeSemigroup], 0,
    function(s)
        Info(InfoWarning,1,
            "Green's relations for infinite semigroups is not supported");
        return fail;
    end);

InstallMethod(GreensJRelation, "for free semigroups", true,
        [IsSemigroup and IsFreeSemigroup], 0,
    function(s)
        Info(InfoWarning,1,
            "Green's relations for infinite semigroups is not supported");
        return fail;
    end);

InstallMethod(GreensDRelation, "for free semigroups", true,
        [IsSemigroup and IsFreeSemigroup], 0,
    function(s)
        Info(InfoWarning,1,
            "Green's relations for infinite semigroups is not supported");
        return fail;
    end);

InstallMethod(GreensHRelation, "for free semigroups", true,
        [IsSemigroup and IsFreeSemigroup], 0,
    function(s)
        Info(InfoWarning,1,
            "Green's relations for infinite semigroups is not supported");
        return fail;
    end);


#############################################################################
##
#O  SemigroupHomomorphismByImagesNC( <mapp> )
##
##  returns a `SemigroupHomomorphism' represented by
## `IsSemigroupHomomorphismByImagesRep'.

InstallMethod(SemigroupHomomorphismByImagesNC, "for a semigroup, semigroup, list", true,
              [IsSemigroup, IsSemigroup, IsList], 0,
function(S, T, imgslist)
local hom, filter;

 if Size(S)<>Length(imgslist) then
    Error("<S> and <T> must have the same size");
  fi;

	#SetAsSSortedList(imgslist, imgslist);
  hom:=rec(imgslist:=imgslist);

Objectify(NewType( GeneralMappingsFamily
    ( ElementsFamily( FamilyObj( S ) ),
      ElementsFamily( FamilyObj( T ) ) ), IsSemigroupHomomorphism
      and IsSemigroupHomomorphismByImagesRep), hom);

  SetSource(hom, S);
  SetRange(hom, T);

  return hom;
end);

#

########
########

InstallMethod(ImagesRepresentative, "for semigroup homomorphism by images",
              FamSourceEqFamElm,
              [IsSemigroupHomomorphism and IsSemigroupHomomorphismByImagesRep, IsMultiplicativeElement],
function(hom, elt)
  return hom!.imgslist[Position(Elements(Source(hom)), elt)];
end);

########
########

InstallMethod(PreImagesRepresentative,  "for semigroup homomorphism by images",
              FamRangeEqFamElm,
              [IsSemigroupHomomorphism and IsSemigroupHomomorphismByImagesRep, IsMultiplicativeElement],
function(hom, x)
  local preimgs, imgs;

  if HasInverseGeneralMapping(hom) then
	  return ImageElm(InverseGeneralMapping(hom), x);
  fi;

  imgs:=hom!.imgslist;

  preimgs:=List([1..Length(imgs)], function(y)
  if imgs[y]=x then
    return Elements(Source(hom))[y];
  else
    return fail;
  fi;
  end);

  return Filtered(preimgs, x-> not x=fail);
end);

########
########

InstallMethod( ViewObj, "for semigroup homomorphism by images",
              [IsSemigroupHomomorphism and IsSemigroupHomomorphismByImagesRep],
function( obj )
  Print( "SemigroupHomomorphismByImages ( ", Source(obj), "->", Range(obj), ")" );
end );

########
########

InstallMethod( PrintObj, "for semigroup homomorphism by images",
              [IsSemigroupHomomorphism and IsSemigroupHomomorphismByImagesRep],
function( obj )
  Print( "SemigroupHomomorphismByImages ( ", Source(obj), "->", Range(obj), ")" );
end );

########
########

InstallMethod(ImagesElm, "for semigroup homomorphism by images",
              FamSourceEqFamElm,
              [IsSemigroupHomomorphism and IsSemigroupHomomorphismByImagesRep, IsMultiplicativeElement],
function( hom, x)
  return [ImagesRepresentative(hom, x)];
end);

########
########

InstallMethod(CompositionMapping2, "for semigroup homomorphism by images",
              #IsIdenticalObj,
              [IsSemigroupHomomorphism and IsSemigroupHomomorphismByImagesRep, IsSemigroupHomomorphism and IsSemigroupHomomorphismByImagesRep],
              0,
function(hom1, hom2)
  local imgslist;

  if not IsSubset(Source(hom2), Range(hom1)) then
    Error("source of <hom2> must contain range of <hom>");
  fi;

  imgslist:=List(hom1!.imgslist, x-> ImageElm(hom2, x));

  return SemigroupHomomorphismByImagesNC(Source(hom1), Range(hom2), imgslist);
end);

########
########

InstallMethod(InverseGeneralMapping, "for semigroup homomorphism by images",
              [IsSemigroupHomomorphism and IsSemigroupHomomorphismByImagesRep and IsInjective and IsSurjective],
              0,
function(iso)

  return SemigroupHomomorphismByImagesNC(Range(iso), Source(iso), List(Elements(Range(iso)), x-> Elements(Source(iso))[Position(iso!.imgslist, x)]));

end);

########
########

InstallMethod(\=, "for semigroup homomorphism by images", IsIdenticalObj,
              [IsSemigroupHomomorphism and IsSemigroupHomomorphismByImagesRep, IsSemigroupHomomorphism and IsSemigroupHomomorphismByImagesRep],
              0,
function(hom1, hom2)

  return ForAll(GeneratorsOfSemigroup(Source(hom1)),
    x -> ImageElm(hom1,x) = ImageElm(hom2,x));

end);

#HACKS

#JDM This is a terrible hack: the way that fp semigroups are implemented in GAP
# means that every time you try to compute anything it first tries to find a
# reduced confluent rewriting system for the presentation. Despite the fact that
# the fp semigroups generated by the FP algorithm already know all their
# elements and have a reduced confluent presentation.

InstallMethod(\<, "for fp semigp elts produced by the Froidure-Pin algorithm", IsIdenticalObj, [IsFpSemigpReducedElt, IsFpSemigpReducedElt],
function(x,y)
  if not x=y then
    return IsShortLexLessThanOrEqual(UnderlyingElement(x), UnderlyingElement(y));
  else
    return false;
  fi;
end);

InstallMethod(\=, "for fp semigp elts produced by the Froidure-Pin algorithm", IsIdenticalObj, [IsFpSemigpReducedElt, IsFpSemigpReducedElt],
function(x,y)
  return UnderlyingElement(x)=UnderlyingElement(y);
end);

InstallMethod(\<, "for fp monoid elts produced by the Froidure-Pin algorithm", IsIdenticalObj, [IsFpMonoidReducedElt, IsFpMonoidReducedElt],
function(x,y)
  if not x=y then
    return IsShortLexLessThanOrEqual(UnderlyingElement(x), UnderlyingElement(y));
  else
    return false;
  fi;
end);

InstallMethod(\=, "for fp monoid elts produced by the Froidure-Pin algorithm", IsIdenticalObj, [IsFpMonoidReducedElt, IsFpMonoidReducedElt],
function(x,y)
  return UnderlyingElement(x)=UnderlyingElement(y);
end);