1/* Part of SWI-Prolog 2 3 Author: Jan Wielemaker 4 E-mail: J.Wielemaker@cs.vu.nl 5 WWW: http://www.swi-prolog.org 6 Copyright (C): 2014-2015, VU University Amsterdam 7 8 This program is free software; you can redistribute it and/or 9 modify it under the terms of the GNU General Public License 10 as published by the Free Software Foundation; either version 2 11 of the License, or (at your option) any later version. 12 13 This program is distributed in the hope that it will be useful, 14 but WITHOUT ANY WARRANTY; without even the implied warranty of 15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 16 GNU General Public License for more details. 17 18 You should have received a copy of the GNU General Public 19 License along with this library; if not, write to the Free Software 20 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 21 22 As a special exception, if you link this library with other files, 23 compiled with a Free Software compiler, to produce an executable, this 24 library does not by itself cause the resulting executable to be covered 25 by the GNU General Public License. This exception does not however 26 invalidate any other reasons why the executable file might be covered by 27 the GNU General Public License. 28*/ 29 30:- module(swish_render_prolog, 31 [ term_rendering//3 % +Term, +Vars, +Options 32 ,clauses//1 33 ]). 34:- use_module(library(apply)). 35:- use_module(library(lists)). 36:- use_module(library(option)). 37:- use_module(library(http/html_write)). 38:- use_module(library(http/term_html)). 39:- use_module(library(slipcover)). 40:- use_module('../render'). 41 42:- register_renderer(prolog, "Render a logic program"). 43 44/** <module> SWISH table renderer 45 46Render table-like data. 47*/ 48 49%% term_rendering(+Term, +Vars, +Options)// 50% 51% Renders Term as a table. This renderer recognises several 52% representations of table-like data: 53% 54% $ A list of terms of equal arity : 55% $ A list of lists of equal length : 56% 57% @tbd: recognise more formats 58 59term_rendering(Term, _Vars, _Options) --> 60 {is_list_of_clauses(Term) 61 }, !, 62 html(pre(\clauses(Term))). 63 64clauses([]) --> []. 65clauses([H1|T]) --> 66 { 67 copy_term(H1,HC), 68 numbervars(HC,0,_), 69 (HC=(H:-B)-> 70 list2and(BL,B) 71 ; 72 BL=[], 73 H=HC 74 ) 75 }, 76 single_clause(H,BL), 77 clauses(T). 78 79single_clause(H,B)--> 80 {format(atom(I),' :-~n',[])}, 81 head(H,B,I). 82 83head(H,[],_I)--> 84 {format(atom(A),"~q.~n~n",[H])},!, 85 [A]. 86 87head(H,B,I)--> 88 {format(atom(A),"~q",[H])},!, 89 [A,I], 90 body(B). 91 92 93body([])--> 94 {format(atom(A)," true.~n~n",[])}, 95 [A]. 96 97body([H])--> 98 {format(atom(A)," ~q.~n~n",[H])},!, 99 [A]. 100 101body([H|T])--> 102 {format(atom(A)," ~q,~n",[H])}, 103 [A],body(T). 104 105 106 107 108 109 110%% is_list_of_terms(@Term, -Rows, -Cols) is semidet. 111% 112% Recognises a list of terms with the same functor and non-zero 113% ariry. 114 115is_list_of_clauses(Term) :- 116 is_list(Term), Term \== []