Skip to content

Commit f701aff

Browse files
authored
Merge pull request #1524 from zickgraf/master
Allow to specify full data types for variables in logic templates
2 parents 8919ff3 + 6dfa679 commit f701aff

File tree

4 files changed

+94
-28
lines changed

4 files changed

+94
-28
lines changed

CompilerForCAP/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 := "CompilerForCAP",
1212
Subtitle := "Speed up and verify categorical algorithms",
13-
Version := "2023.12-07",
13+
Version := "2023.12-08",
1414
Date := (function ( ) if IsBound( GAPInfo.SystemEnvironment.GAP_PKG_RELEASE_DATE ) then return GAPInfo.SystemEnvironment.GAP_PKG_RELEASE_DATE; else return Concatenation( ~.Version{[ 1 .. 4 ]}, "-", ~.Version{[ 6, 7 ]}, "-01" ); fi; end)( ),
1515
License := "GPL-2.0-or-later",
1616

CompilerForCAP/gap/LogicTemplates.gd

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@ DeclareGlobalFunction( "CAP_JIT_INTERNAL_TREE_MATCHES_TEMPLATE_TREE" );
1717
#! Logic templates are records with the following entries:
1818
#! * `src_template` and `dst_template` (required): strings containing valid GAP code defining expressions
1919
#! * `variable_names` (required): a list of strings (which must not be keywords in GAP)
20-
#! * `variable_filters` (optional): a list of filters with the same length as `variable_names`, defaults to a list of `IsObject`
20+
#! * `variable_filters` (optional): a list of filters or data types (see <Ref Func="CapJitInferredDataTypes" />)
21+
#! with the same length as `variable_names`, defaults to a list of `IsObject`
2122
#! * `new_funcs` (optional): a list of lists of strings, defaults to the empty list
2223
#! * `needed_packages` (optional): a list of pairs (i.e. lists with two entries) of strings, defaults to the empty list
2324
#! * `debug` (optional): a boolean
@@ -29,7 +30,7 @@ DeclareGlobalFunction( "CAP_JIT_INTERNAL_TREE_MATCHES_TEMPLATE_TREE" );
2930
#! * When trying to find an occurence of `src_template` in a tree, all strings occuring in the list `variable_names`
3031
#! are considered as variables, i.e., they match any value in the tree. If a variable occurs
3132
#! multiple times, the corresponding parts of the tree must be equal.
32-
#! The template is only applied if all values match the corresponding filters in `variable_filters`.
33+
#! The template is only applied if all values match the corresponding filters or data types in `variable_filters`.
3334
#! * For each function in `dst_template`, <Ref Func="CapJitAppliedLogicTemplates" /> tries to find a corresponding function in
3435
#! `src_template`. The functions are matched by comparing the lists of names of function arguments. If for a function in
3536
#! `dst_template` no corresponding function in `src_template` exists, you have to add the list of names of function arguments

CompilerForCAP/gap/LogicTemplates.gi

Lines changed: 69 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -60,13 +60,6 @@ InstallGlobalFunction( CAP_JIT_INTERNAL_ENHANCE_LOGIC_TEMPLATE, function ( templ
6060

6161
od;
6262

63-
if IsBound( template.variable_filters ) and Length( template.variable_names ) <> Length( template.variable_filters ) then
64-
65-
# COVERAGE_IGNORE_NEXT_LINE
66-
Error( "the length of the record entries variable_names and variable_filters of a logic template must be equal" );
67-
68-
fi;
69-
7063
if Last( NormalizedWhitespace( template.src_template ) ) = ';' then
7164

7265
# COVERAGE_IGNORE_NEXT_LINE
@@ -112,6 +105,20 @@ InstallGlobalFunction( CAP_JIT_INTERNAL_ENHANCE_LOGIC_TEMPLATE, function ( templ
112105

113106
fi;
114107

108+
if Length( template.variable_names ) <> Length( template.variable_filters ) then
109+
110+
# COVERAGE_IGNORE_NEXT_LINE
111+
Error( "the length of the record entries variable_names and variable_filters of a logic template must be equal" );
112+
113+
fi;
114+
115+
if not ForAll( template.variable_filters, f -> IsFilter( f ) or IsRecord( f ) ) then
116+
117+
# COVERAGE_IGNORE_NEXT_LINE
118+
Error( "the entries of variable_filters must be filters or records" );
119+
120+
fi;
121+
115122
# default new funcs: none
116123
if not IsBound( template.new_funcs ) then
117124

@@ -636,7 +643,7 @@ InstallGlobalFunction( CAP_JIT_INTERNAL_TREE_MATCHES_TEMPLATE_TREE, function ( t
636643
end;
637644

638645
result_func := function ( template_tree, result, keys, tree )
639-
local var_number, filter, key;
646+
local var_number, filter_or_data_type, key;
640647

641648
if debug then
642649
# COVERAGE_IGNORE_BLOCK_START
@@ -661,38 +668,75 @@ InstallGlobalFunction( CAP_JIT_INTERNAL_TREE_MATCHES_TEMPLATE_TREE, function ( t
661668

662669
if not IsBound( variables[var_number] ) then
663670

664-
filter := variable_filters[var_number];
671+
filter_or_data_type := variable_filters[var_number];
665672

666-
if IsIdenticalObj( filter, IsObject ) or (IsBound( tree.data_type ) and IsSpecializationOfFilter( filter, tree.data_type.filter )) then
667-
668-
variables[var_number] := tree;
673+
if IsRecord( filter_or_data_type ) then
669674

670-
if debug then
671-
# COVERAGE_IGNORE_BLOCK_START
672-
Display( "matched via variable1" );
673-
Display( true );
674-
# COVERAGE_IGNORE_BLOCK_END
675+
if IsBound( tree.data_type ) and tree.data_type = filter_or_data_type then
676+
677+
variables[var_number] := tree;
678+
679+
if debug then
680+
# COVERAGE_IGNORE_BLOCK_START
681+
Display( "matched via new variable with data type" );
682+
Display( true );
683+
# COVERAGE_IGNORE_BLOCK_END
684+
fi;
685+
686+
return true;
687+
688+
else
689+
690+
if debug then
691+
# COVERAGE_IGNORE_BLOCK_START
692+
Display( "type could not be inferred or did not match" );
693+
# COVERAGE_IGNORE_BLOCK_END
694+
fi;
695+
696+
return false;
697+
675698
fi;
676699

677-
return true;
678700

679-
else
701+
elif IsFilter( filter_or_data_type ) then
680702

681-
if debug then
682-
# COVERAGE_IGNORE_BLOCK_START
683-
Display( "type could not be inferred or did not match" );
684-
# COVERAGE_IGNORE_BLOCK_END
703+
if IsIdenticalObj( filter_or_data_type, IsObject ) or (IsBound( tree.data_type ) and IsSpecializationOfFilter( filter_or_data_type, tree.data_type.filter )) then
704+
705+
variables[var_number] := tree;
706+
707+
if debug then
708+
# COVERAGE_IGNORE_BLOCK_START
709+
Display( "matched via new variable with filter" );
710+
Display( true );
711+
# COVERAGE_IGNORE_BLOCK_END
712+
fi;
713+
714+
return true;
715+
716+
else
717+
718+
if debug then
719+
# COVERAGE_IGNORE_BLOCK_START
720+
Display( "type could not be inferred or did not match" );
721+
# COVERAGE_IGNORE_BLOCK_END
722+
fi;
723+
724+
return false;
725+
685726
fi;
686727

687-
return false;
728+
else
729+
730+
# COVERAGE_IGNORE_NEXT_LINE
731+
Error( "this should never happen" );
688732

689733
fi;
690734

691735
else
692736

693737
if debug then
694738
# COVERAGE_IGNORE_BLOCK_START
695-
Display( "matched via variable2" );
739+
Display( "matched via existing variable" );
696740
Display( CapJitIsEqualForEnhancedSyntaxTrees( variables[var_number], tree ) );
697741
# COVERAGE_IGNORE_BLOCK_END
698742
fi;

CompilerForCAP/tst/LogicTemplates.tst

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -324,5 +324,26 @@ end
324324
gap> Length( CAP_JIT_LOGIC_TEMPLATES ) = orig_number_of_logic_templates;
325325
true
326326

327+
# test full data types in variable_filters
328+
gap> template := rec(
329+
> variable_names := [ "list" ],
330+
> variable_filters := [ CapJitDataTypeOfListOf( IsStringRep ) ],
331+
> src_template := "list{[ 1 ]}",
332+
> dst_template := "[ list[1] ]",
333+
> );;
334+
gap> CAP_JIT_INTERNAL_ENHANCE_LOGIC_TEMPLATE( template );
335+
336+
#
337+
gap> Display( applied_logic_template_to_func( { } -> [ "1", "2" ]{[ 1 ]}, template, Pair( [ ], fail ) ) );
338+
function ( )
339+
return [ [ "1", "2" ][1] ];
340+
end
341+
342+
#
343+
gap> Display( applied_logic_template_to_func( { } -> [ 1, 2 ]{[ 1 ]}, template, Pair( [ ], fail ) ) );
344+
function ( )
345+
return [ 1, 2 ]{[ 1 ]};
346+
end
347+
327348
#
328349
gap> STOP_TEST( "LogicTemplates" );

0 commit comments

Comments
 (0)