So here I made some examples to remember what was actually the problem:
#!/usr/local/bin/swipl -s
test_001 :- true
, L = [ #{a:1}, #{a:2}]
, bagof( M.a, member( M, L), L2)
.
test_002 :- true
, L = [ #{a:1}, #{a:2}]
, D = #{a:_}
, bagof( M.a, ( D = M, member( M, L)), L2)
.
test_003 :- true
, L = [ #{a:1}, #{a:2}]
, D = #{a:_}
, bagof( M.a, ( member( M, L), M >:< D), L2)
.
test_004 :- true % works
, L = [ #{a:1}, #{a:2}]
, D = #{a:_}
, bagof( D.a, ( member( M, L), M >:< D), L2)
.
test_005 :- true
, L = [ #{a:1}, #{a:2}]
, bagof( M.a, ( member( M, L), M >:< #{a:_} ), L2)
.
test_006 :- true % works
, L = [ #{a:1}, #{a:2}]
, bagof( A, ( member( M, L), A = M.a ), L2)
.
test_007 :- true
, L = [ #{a:1}, #{a:2}]
, TERM1 = member( M, L)
, TERM2 = M.a
, bagof( TERM2, TERM1, L2)
.
test_008 :- true
, L = [ #{a:1}, #{a:2}]
, TERM1 = member( M, L)
% , TERM2 = M.a
, atom_to_term( 'M.a', TERM2, Vs), member( 'M'=M, Vs)
, mi( bagof( TERM2, TERM1, L2), T)
, T
, writeln( L2)
.
mi( bagof( T, G, L), TERM) :- true
, mi(T, T1-T2)
, TERM = bagof( T2, T1^(G, T1), L)
.
% mi( D.K, T1-T2) :- T1 = get_dict( K, D, T2) . % error when called
mi( D+K, T1-T2) :- T1 = get_dict( K, D, T2) .
test_009 :- true % works
, L = [ #{a:1}, #{a:2}]
, TERM1 = member( M, L)
% , TERM2 = M.a
, atom_to_term( 'M+a', TERM2, Vs), member( 'M'=M, Vs)
, mi( bagof( TERM2, TERM1, L2), T)
, T
, writeln( L2)
.
From that 9 examples 3 work and the last uses a simplified metainterpreter.
In the test_009 I switched from ‘.’ to ‘+’ to make it work. test_008 could work if I were able to temporarily disable a specific portion of the expansion rules (I don’t know which one exactly because there are more). It could actually also be possible to disable the expansion mechanisms at all while mi is running.