Skip to content

Commit 9290d59

Browse files
authored
Merge pull request #1029 from zickgraf/master
Remove ability to add derivations with extra filters
2 parents 3dc26db + d2250f1 commit 9290d59

File tree

4 files changed

+74
-128
lines changed

4 files changed

+74
-128
lines changed

CAP/PackageInfo.g

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ SetPackageInfo( rec(
1010

1111
PackageName := "CAP",
1212
Subtitle := "Categories, Algorithms, Programming",
13-
Version := "2022.09-07",
13+
Version := "2022.09-08",
1414
Date := Concatenation( "01/", ~.Version{[ 6, 7 ]}, "/", ~.Version{[ 1 .. 4 ]} ),
1515
License := "GPL-2.0-or-later",
1616

CAP/gap/Derivations.gd

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -55,23 +55,16 @@ DeclareCategory( "IsDerivedMethod", IsObject );
5555
#! using this derivation. Unless there is any particular reason
5656
#! to regard the derivation as exceedingly expensive, this number
5757
#! should be <C>1</C>.
58-
#! The argument <A>implementations_with_extra_filters</A> contains
59-
#! one or more functions with the actual implementation of the
60-
#! derived method, together with lists of extra argument filters
61-
#! for each function. The argument is a list with entries of the
62-
#! form <C>[fun, filters]</C>, where <C>fun</C> is a function and
63-
#! <C>filters</C> is a (not necessarily dense) list of argument
64-
#! filters. If only one function is given, then <C>filters</C>
65-
#! should be the empty list; in this case the argument's value
66-
#! would be [[fun,[]]], where <C>fun</C> is the function.
58+
#! The argument <A>func</A> contains the actual implementation of the
59+
#! derived method.
6760
#! The argument <A>category_filter</A> is a filter describing
6861
#! which categories the derivation is valid for. If it is valid
6962
#! for all categories, then this argument should have the value
7063
#! <C>IsCapCategory</C>.
71-
#! @Arguments name, target_op, used_ops_with_multiples, weight, implementations_with_extra_filters, category_filter
64+
#! @Arguments name, target_op, used_ops_with_multiples, weight, func, category_filter
7265
DeclareOperation( "MakeDerivation",
7366
[ IsString, IsFunction, IsDenseList,
74-
IsPosInt, IsDenseList, IsFunction ] );
67+
IsPosInt, IsFunction, IsFunction ] );
7568

7669
#! @Description
7770
#! The name of the derivation. This is a name identifying this
@@ -86,10 +79,9 @@ DeclareAttribute( "DerivationName", IsDerivedMethod );
8679
DeclareAttribute( "DerivationWeight", IsDerivedMethod );
8780

8881
#! @Description
89-
#! The implementation(s) of the derivation, together with lists
90-
#! of extra filters for each implementation.
82+
#! The implementation of the derivation.
9183
#! @Arguments d
92-
DeclareAttribute( "DerivationFunctionsWithExtraFilters", IsDerivedMethod );
84+
DeclareAttribute( "DerivationFunction", IsDerivedMethod );
9385

9486
#! @Description
9587
#! Filter describing which categories the derivation is valid for.

CAP/gap/Derivations.gi

Lines changed: 40 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -34,15 +34,15 @@ InstallGlobalFunction( "DeactivateDerivationInfo",
3434
end );
3535

3636
InstallMethod( MakeDerivation,
37-
[ IsString, IsFunction, IsDenseList, IsPosInt, IsDenseList, IsFunction ],
37+
[ IsString, IsFunction, IsDenseList, IsPosInt, IsFunction, IsFunction ],
3838

39-
function( name, target_op, used_op_names_with_multiples_and_category_getters, weight, implementations_with_extra_filters, category_filter )
39+
function( name, target_op, used_op_names_with_multiples_and_category_getters, weight, func, category_filter )
4040

4141
return ObjectifyWithAttributes(
4242
rec( ), NewType( TheFamilyOfDerivations, IsDerivedMethodRep ),
4343
DerivationName, name,
4444
DerivationWeight, weight,
45-
DerivationFunctionsWithExtraFilters, implementations_with_extra_filters,
45+
DerivationFunction, func,
4646
CategoryFilter, category_filter,
4747
TargetOperation, NameFunction( target_op ),
4848
UsedOperationsWithMultiplesAndCategoryGetters, used_op_names_with_multiples_and_category_getters
@@ -80,7 +80,7 @@ end );
8080
InstallMethod( InstallDerivationForCategory,
8181
[ IsDerivedMethod, IsPosInt, IsCapCategory ],
8282
function( d, weight, C )
83-
local method_name, implementation_list, add_method, add_name, general_filter_list,
83+
local method_name, func, add_method, add_name, general_filter_list,
8484
installation_name, nr_arguments, cache_name, current_filters, current_implementation,
8585
function_called_before_installation;
8686

@@ -92,7 +92,7 @@ function( d, weight, C )
9292
DerivationName( d ), "\n" ) );
9393

9494
method_name := TargetOperation( d );
95-
implementation_list := DerivationFunctionsWithExtraFilters( d );
95+
func := DerivationFunction( d );
9696
add_name := Concatenation( "Add", method_name );
9797
add_method := ValueGlobal( add_name );
9898

@@ -102,7 +102,9 @@ function( d, weight, C )
102102

103103
fi;
104104

105-
add_method( C, implementation_list, weight : IsDerivation := true );
105+
# use the add method with signature IsCapCategory, IsList, IsInt to avoid
106+
# the convenience for AddZeroObject etc.
107+
add_method( C, [ Pair( func, [ ] ) ], weight : IsDerivation := true );
106108

107109
end );
108110

@@ -176,7 +178,7 @@ end );
176178
InstallMethod( AddDerivation,
177179
[ IsDerivedMethodGraphRep, IsDerivedMethod ],
178180
function( G, d )
179-
local method_name, filter_list, number_of_proposed_arguments, current_function_argument_number, current_additional_filter_list_length, impl, x;
181+
local method_name, filter_list, number_of_proposed_arguments, current_function_argument_number, x;
180182

181183
if IsIdenticalObj( G, CAP_INTERNAL_DERIVATION_GRAPH ) then
182184

@@ -192,23 +194,12 @@ function( G, d )
192194

193195
number_of_proposed_arguments := Length( filter_list );
194196

195-
for impl in DerivationFunctionsWithExtraFilters( d ) do
196-
197-
current_function_argument_number := NumberArgumentsFunction( impl[ 1 ] );
198-
199-
if current_function_argument_number >= 0 and current_function_argument_number <> number_of_proposed_arguments then
200-
Error( "While adding a derivation for ", method_name, ": given function has ", String( current_function_argument_number ),
201-
" arguments but should have ", String( number_of_proposed_arguments ) );
202-
fi;
203-
204-
current_additional_filter_list_length := Length( impl[ 2 ] );
205-
206-
if current_additional_filter_list_length > 0 and current_additional_filter_list_length <> number_of_proposed_arguments then
207-
Error( "While adding a derivation for ", method_name, ": there are ", String( current_additional_filter_list_length ),
208-
" additional filters but there should be ", String( number_of_proposed_arguments ), " (or none)" );
209-
fi;
210-
211-
od;
197+
current_function_argument_number := NumberArgumentsFunction( DerivationFunction( d ) );
198+
199+
if current_function_argument_number >= 0 and current_function_argument_number <> number_of_proposed_arguments then
200+
Error( "While adding a derivation for ", method_name, ": given function has ", String( current_function_argument_number ),
201+
" arguments but should have ", String( number_of_proposed_arguments ) );
202+
fi;
212203

213204
if NumberArgumentsFunction( CategoryFilter( d ) ) = 0 or NumberArgumentsFunction( CategoryFilter( d ) ) > 1 then
214205

@@ -230,45 +221,17 @@ end );
230221
InstallMethod( AddDerivation,
231222
[ IsDerivedMethodGraph, IsFunction, IsFunction ],
232223

233-
function( graph, target_op, implementations_with_extra_filters )
224+
function( graph, target_op, func )
234225

235-
AddDerivation( graph,
236-
target_op,
237-
[ ],
238-
[ [ implementations_with_extra_filters, [ ] ] ] );
239-
240-
end );
241-
242-
InstallMethod( AddDerivation,
243-
[ IsDerivedMethodGraph, IsFunction, IsDenseList ],
244-
245-
function( graph, target_op, implementations_with_extra_filters )
246-
247-
AddDerivation( graph,
248-
target_op,
249-
[ ],
250-
[ [ implementations_with_extra_filters, [ ] ] ] );
226+
AddDerivation( graph, target_op, [ ], func );
251227

252228
end );
253229

254230
InstallMethod( AddDerivation,
255231
[ IsDerivedMethodGraph, IsFunction, IsDenseList, IsFunction ],
256232

257-
function( graph, target_op, used_ops_with_multiples,
258-
implementations_with_extra_filters )
259-
260-
AddDerivation( graph,
261-
target_op,
262-
used_ops_with_multiples,
263-
[ [ implementations_with_extra_filters, [ ] ] ] );
264-
265-
end );
266-
267-
InstallMethod( AddDerivation,
268-
[ IsDerivedMethodGraph, IsFunction, IsDenseList, IsDenseList ],
269-
270-
function( graph, target_op, used_ops_with_multiples_and_category_getters, implementations_with_extra_filters )
271-
local weight, category_filter, description, loop_multiplier, category_getters, function_called_before_installation, operations_in_graph, collected_list, current_list, used_op_names_with_multiples_and_category_getters, derivation, current_implementation, x;
233+
function( graph, target_op, used_ops_with_multiples_and_category_getters, func )
234+
local weight, category_filter, description, loop_multiplier, category_getters, function_called_before_installation, operations_in_graph, collected_list, used_op_names_with_multiples_and_category_getters, derivation, x;
272235

273236
weight := CAP_INTERNAL_RETURN_OPTION_OR_DEFAULT( "Weight", 1 );
274237
category_filter := CAP_INTERNAL_RETURN_OPTION_OR_DEFAULT( "CategoryFilter", IsCapCategory );
@@ -280,14 +243,7 @@ InstallMethod( AddDerivation,
280243
## get used ops
281244
operations_in_graph := Operations( graph );
282245

283-
collected_list := [ ];
284-
285-
for current_implementation in implementations_with_extra_filters do
286-
287-
current_list := CAP_INTERNAL_FIND_APPEARANCE_OF_SYMBOL_IN_FUNCTION( current_implementation[ 1 ], operations_in_graph, loop_multiplier, CAP_INTERNAL_METHOD_RECORD_REPLACEMENTS, category_getters );
288-
collected_list := CAP_INTERNAL_MERGE_PRECONDITIONS_LIST( collected_list, current_list );
289-
290-
od;
246+
collected_list := CAP_INTERNAL_FIND_APPEARANCE_OF_SYMBOL_IN_FUNCTION( func, operations_in_graph, loop_multiplier, CAP_INTERNAL_METHOD_RECORD_REPLACEMENTS, category_getters );
291247

292248
if IsEmpty( used_ops_with_multiples_and_category_getters ) then
293249

@@ -352,7 +308,7 @@ InstallMethod( AddDerivation,
352308
target_op,
353309
used_op_names_with_multiples_and_category_getters,
354310
weight,
355-
implementations_with_extra_filters,
311+
func,
356312
category_filter );
357313

358314
if function_called_before_installation <> false then
@@ -365,6 +321,24 @@ InstallMethod( AddDerivation,
365321

366322
end );
367323

324+
InstallMethod( AddDerivation,
325+
[ IsDerivedMethodGraph, IsFunction, IsDenseList ],
326+
327+
function( graph, target_op, implementations_with_extra_filters )
328+
329+
Error( "passing a list of functions to `AddDerivation` is not supported anymore" );
330+
331+
end );
332+
333+
InstallMethod( AddDerivation,
334+
[ IsDerivedMethodGraph, IsFunction, IsDenseList, IsDenseList ],
335+
336+
function( graph, target_op, used_ops_with_multiples, implementations_with_extra_filters )
337+
338+
Error( "passing a list of functions to `AddDerivation` is not supported anymore" );
339+
340+
end );
341+
368342
InstallGlobalFunction( AddDerivationToCAP,
369343

370344
function( arg )
@@ -975,7 +949,7 @@ InstallGlobalFunction( DerivationsOfMethodByCategory,
975949

976950
od;
977951

978-
currently_installed_funcs := DerivationFunctionsWithExtraFilters( current_derivation );
952+
currently_installed_funcs := [ Pair( DerivationFunction( current_derivation ), [ ] ) ];
979953

980954
fi;
981955

CAP/gap/Finalize.gi

Lines changed: 27 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ InstallValue( CAP_INTERNAL_FINAL_DERIVATION_LIST,
99
BindGlobal( "CAP_INTERNAL_FINAL_DERIVATION_SANITY_CHECK",
1010

1111
function( derivation )
12-
local methods_to_check, method_name, filter_list, number_of_proposed_arguments, current_function_argument_number, current_additional_filter_list_length, method, impl;
12+
local methods_to_check, method_name, filter_list, number_of_proposed_arguments, current_function_argument_number, method;
1313

1414
if PositionSublist( String( derivation.category_filter ), "CanCompute" ) <> fail then
1515

@@ -23,7 +23,7 @@ BindGlobal( "CAP_INTERNAL_FINAL_DERIVATION_SANITY_CHECK",
2323

2424
fi;
2525

26-
methods_to_check := Concatenation( [ [ derivation.target_op, derivation.function_list ] ], derivation.additional_functions );
26+
methods_to_check := Concatenation( [ [ derivation.target_op, derivation.func ] ], derivation.additional_functions );
2727

2828
for method in methods_to_check do
2929

@@ -46,23 +46,12 @@ BindGlobal( "CAP_INTERNAL_FINAL_DERIVATION_SANITY_CHECK",
4646

4747
number_of_proposed_arguments := Length( filter_list );
4848

49-
for impl in method[2] do
50-
51-
current_function_argument_number := NumberArgumentsFunction( impl[ 1 ] );
52-
53-
if current_function_argument_number >= 0 and current_function_argument_number <> number_of_proposed_arguments then
54-
Error( "While adding a final derivation for ", method_name, ": given function has ", String( current_function_argument_number ),
55-
" arguments but should have ", String( number_of_proposed_arguments ) );
56-
fi;
57-
58-
current_additional_filter_list_length := Length( impl[ 2 ] );
59-
60-
if current_additional_filter_list_length > 0 and current_additional_filter_list_length <> number_of_proposed_arguments then
61-
Error( "While adding a final derivation for ", method_name, ": there are ", String( current_additional_filter_list_length ),
62-
" additional filters but there should be ", String( number_of_proposed_arguments ), " (or none)" );
63-
fi;
64-
65-
od;
49+
current_function_argument_number := NumberArgumentsFunction( method[2] );
50+
51+
if current_function_argument_number >= 0 and current_function_argument_number <> number_of_proposed_arguments then
52+
Error( "While adding a final derivation for ", method_name, ": given function has ", String( current_function_argument_number ),
53+
" arguments but should have ", String( number_of_proposed_arguments ) );
54+
fi;
6655

6756
od;
6857

@@ -77,11 +66,11 @@ end );
7766

7867
InstallGlobalFunction( AddFinalDerivation,
7968

80-
function( target_op, can_compute, cannot_compute, func_list, additional_functions... )
81-
local final_derivation, loop_multiplier, category_getters, function_called_before_installation, operations_in_graph, operations_to_install, collected_list, current_list, union_of_collected_lists, used_op_names_with_multiples_and_category_getters, i, current_implementation, current_additional_func, x;
69+
function( target_op, can_compute, cannot_compute, func, additional_functions... )
70+
local final_derivation, loop_multiplier, category_getters, function_called_before_installation, operations_in_graph, operations_to_install, collected_list, union_of_collected_lists, used_op_names_with_multiples_and_category_getters, i, current_additional_func, x;
8271

83-
if IsFunction( func_list ) then
84-
func_list := [ [ func_list, [] ] ];
72+
if IsList( func ) then
73+
Error( "passing a list of functions to `AddFinalDerivation` is not supported anymore" );
8574
fi;
8675

8776
final_derivation := rec( );
@@ -94,8 +83,8 @@ InstallGlobalFunction( AddFinalDerivation,
9483
function_called_before_installation := CAP_INTERNAL_RETURN_OPTION_OR_DEFAULT( "FunctionCalledBeforeInstallation", false );
9584

9685
for i in [ 1 .. Length( additional_functions ) ] do
97-
if IsFunction( additional_functions[ i ][ 2 ] ) then
98-
additional_functions[ i ][ 2 ] := [ [ additional_functions[ i ][ 2 ], [ ] ] ];
86+
if IsList( additional_functions[i][2] ) then
87+
Error( "passing lists of functions to `AddFinalDerivation` is not supported anymore" );
9988
fi;
10089
od;
10190
final_derivation.additional_functions := additional_functions;
@@ -105,15 +94,7 @@ InstallGlobalFunction( AddFinalDerivation,
10594
## Find symbols in functions
10695
operations_to_install := [ ];
10796

108-
collected_list := [ ];
109-
110-
for current_implementation in func_list do
111-
112-
current_list := CAP_INTERNAL_FIND_APPEARANCE_OF_SYMBOL_IN_FUNCTION( current_implementation[ 1 ], operations_in_graph, loop_multiplier, CAP_INTERNAL_METHOD_RECORD_REPLACEMENTS, category_getters );
113-
collected_list := CAP_INTERNAL_MERGE_PRECONDITIONS_LIST( collected_list, current_list );
114-
115-
od;
116-
97+
collected_list := CAP_INTERNAL_FIND_APPEARANCE_OF_SYMBOL_IN_FUNCTION( func, operations_in_graph, loop_multiplier, CAP_INTERNAL_METHOD_RECORD_REPLACEMENTS, category_getters );
11798
final_derivation.used_ops_with_multiples := collected_list;
11899

119100
Add( operations_to_install, NameFunction( target_op ) );
@@ -122,22 +103,15 @@ InstallGlobalFunction( AddFinalDerivation,
122103

123104
for current_additional_func in final_derivation.additional_functions do
124105

125-
collected_list := [];
126-
127-
for current_implementation in current_additional_func[ 2 ] do
128-
129-
current_list := CAP_INTERNAL_FIND_APPEARANCE_OF_SYMBOL_IN_FUNCTION( current_implementation[ 1 ], operations_in_graph, loop_multiplier, CAP_INTERNAL_METHOD_RECORD_REPLACEMENTS, category_getters );
130-
collected_list := CAP_INTERNAL_MERGE_PRECONDITIONS_LIST( collected_list, current_list );
131-
132-
od;
106+
collected_list := CAP_INTERNAL_FIND_APPEARANCE_OF_SYMBOL_IN_FUNCTION( current_additional_func[2], operations_in_graph, loop_multiplier, CAP_INTERNAL_METHOD_RECORD_REPLACEMENTS, category_getters );
133107

134-
current_additional_func[ 3 ] := collected_list;
108+
current_additional_func[3] := collected_list;
135109

136110
# Operations may use operations from the same final derivation as long as the latter are installed before the former.
137111
# In this case, the used operations are no preconditions and thus should not go into union_of_collected_lists.
138112
collected_list := Filtered( collected_list, x -> not x[1] in operations_to_install );
139113

140-
Add( operations_to_install, NameFunction( current_additional_func[ 1 ] ) );
114+
Add( operations_to_install, NameFunction( current_additional_func[1] ) );
141115

142116
union_of_collected_lists := CAP_INTERNAL_MERGE_PRECONDITIONS_LIST( union_of_collected_lists, collected_list );
143117

@@ -199,7 +173,7 @@ InstallGlobalFunction( AddFinalDerivation,
199173
final_derivation.target_op := target_op;
200174
final_derivation.can_compute := used_op_names_with_multiples_and_category_getters;
201175
final_derivation.cannot_compute := cannot_compute;
202-
final_derivation.function_list := func_list;
176+
final_derivation.func := func;
203177
final_derivation.function_called_before_installation := function_called_before_installation;
204178

205179
CAP_INTERNAL_FINAL_DERIVATION_SANITY_CHECK( final_derivation );
@@ -405,7 +379,9 @@ InstallMethod( Finalize,
405379
## Add method
406380
add_name := ValueGlobal( Concatenation( [ "Add", NameFunction( current_final_derivation.target_op ) ] ) );
407381

408-
add_name( category, current_final_derivation.function_list, weight : IsFinalDerivation := true );
382+
# use the add method with signature IsCapCategory, IsList, IsInt to avoid
383+
# the convenience for AddZeroObject etc.
384+
add_name( category, [ Pair( current_final_derivation.func, [ ] ) ], weight : IsFinalDerivation := true );
409385

410386
for current_additional_func in current_final_derivation.additional_functions do
411387

@@ -455,7 +431,11 @@ InstallMethod( Finalize,
455431
current_final_derivation.description, "\n" ) );
456432

457433
add_name := ValueGlobal( Concatenation( [ "Add", NameFunction( current_additional_func[ 1 ] ) ] ) );
458-
add_name( category, current_additional_func[ 2 ], weight : IsFinalDerivation := true );
434+
435+
436+
# use the add method with signature IsCapCategory, IsList, IsInt to avoid
437+
# the convenience for AddZeroObject etc.
438+
add_name( category, [ Pair( current_additional_func[2], [ ] ) ], weight : IsFinalDerivation := true );
459439

460440
od;
461441

0 commit comments

Comments
 (0)