prologcyk

How to produce products on the CYK table in prolog?


We are using the concept of CYK table formation to produce these results on Prolog. Here are some sample outputs for product_c(+Cell1, +Cell2, -Product):

?- product_c(["A","B"],["C","D"],What).
What = ["AC", "AD", "BC", "BD"].
?- product_c(["A"],[],What).
What = [].

I've tried using string_concat, but that gives me results like:

What = ["A", "B", "C", "D"].

I'm not sure how to go about this problem. Any help is much appreciated.


Solution

  • You got here kind of type mix up. The quickest fix would be like taking the cartesian product from here Cartesian prod and then concatenating the result list.

    list_List_CartProd(L1,L2,L3):- 
         cartProd(L1,L2,L3,L1).
    
    cartProd(_, [], [], _).
    
    cartProd([], [_|T2], L3, L4):-
        cartProd(L4, T2, L3, L4).
    
    cartProd([H1|T1], [H2|T2], [[H1,H2]|T3], L4):-
        cartProd(T1, [H2|T2], T3, L4).
    
    list_concatEl([],[]).
    list_concatEl([X|Xs],[Y|Ys]) :-
        X=[X1,X2],
        string_concat(X1,X2,Y),
        list_concatEl(Xs,Ys).
    
    product_c(L1,L2,L4) :-
        list_List_CartProd(L1,L2,L3),
        list_concatEl(L3,L4).
    

    If we test it now with your cases we get:

    ?- product_c(["A","B"],["C","D"],What).
    What = ["AC", "BC", "AD", "BD"] ;
    false.
    
    ?- product_c(["A"],[],What). 
    What = [] ;
    false.