1/* Part of Extended Libraries for Prolog 2 3 Author: Edison Mera 4 E-mail: efmera@gmail.com 5 WWW: https://github.com/edisonm/xlibrary 6 Copyright (C): 2015, Process Design Center, Breda, The Netherlands. 7 All rights reserved. 8 9 Redistribution and use in source and binary forms, with or without 10 modification, are permitted provided that the following conditions 11 are met: 12 13 1. Redistributions of source code must retain the above copyright 14 notice, this list of conditions and the following disclaimer. 15 16 2. Redistributions in binary form must reproduce the above copyright 17 notice, this list of conditions and the following disclaimer in 18 the documentation and/or other materials provided with the 19 distribution. 20 21 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 22 "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 23 LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 24 FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE 25 COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 26 INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 27 BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 28 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 29 CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 30 LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN 31 ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 32 POSSIBILITY OF SUCH DAMAGE. 33*/ 34 35:- module(termhide, [op(1150, fx, hide)]). 36 37:- use_module(library(sequence_list)). 38:- use_module(library(occurs)). 39:- reexport(library(compound_expand)). 40:- init_expansors. 41 42/* <module> Term hide 43 44Inspired in the declaration ```:- hide Spec``` as described in the paper: 45 46N. Stulova, J. F. Morales, M. V. Hermenegildo. Towards Run-time Checks 47Simplification via Term Hiding. Technical Communications of the 33rd 48International Conference on Logic Programming (ICLP 2017), OpenAccess Series in 49Informatics (OASIcs), Vol. 58, pages 91-93, Schloss Dagstuhl-Leibniz-Zentrum 50fuer Informatik, 2017. (Extended Abstract). 51 52But note that the motivation to implement this is different than the one in such 53paper. What we want is to avoid irrestricted access to data structures out of a 54given module. 55*/ 56 57:- multifile '$termhide'/2. 58 59term_expansion_decl(hide(PIS), ClauseL) :- 60 prolog_load_context(module, M), 61 sequence_list(PIS, List, []), 62 findall(termhide:'$termhide'(H, M), 63 ( member(F/A, List), 64 functor(H, F, A) 65 ), ClauseL). 66 67scan(Scope, Goal) :- 68 prolog_load_context(module, C), 69 ( Scope = goal, 70 predicate_property(C:Goal, meta_predicate(Meta)) 71 ->strip_module(C:Goal, I, Head), 72 arg(N, Meta, Spec), 73 \+ ( integer(Spec), 74 Spec >= 0 75 ), 76 arg(N, Head, Term), 77 ( Spec = (:) 78 ->strip_module(I:Term, M, Arg) 79 ; M = I, 80 Head = Arg 81 ) 82 ; strip_module(C:Goal, M, Arg) 83 ), 84 sub_term(Sub, Arg), 85 nonvar(Sub), 86 '$termhide'(Sub, T), 87 M \= T, 88 print_message(error, format("Explicit usage of hidden term ~w not allowed", [T:Sub])). 89 90term_expansion((:- Decl), Clauses) :- 91 !, 92 term_expansion_decl(Decl, Clauses). 93% Next line reexports this module in cascade, to prevent usage of the term 94% beyond a second level dependency: 95term_expansion(end_of_file, [(:- reexport(library(termhide))), end_of_file]) :- 96 \+ prolog_load_context(module, termhide), 97 !. 98term_expansion(Head :- _, _) :- 99 !, 100 scan(term, Head), 101 fail. 102term_expansion(Head --> _, _) :- 103 !, 104 scan(term, Head), 105 fail. 106term_expansion(Head, _) :- 107 scan(term, Head), 108 fail. 109 110goal_expansion(Goal, _) :- 111 scan(goal, Goal), 112 fail