1/*  Part of Run-Time Checker for Assertions
    2
    3    Author:        Edison Mera
    4    E-mail:        efmera@gmail.com
    5    WWW:           https://github.com/edisonm/refactor
    6    Copyright (C): 2017, 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(rtchecks_rt,
   36	  [rtchecks_disable/0,
   37           rtchecks_enable/0,
   38           rtcheck_pred/4,
   39           rtcheck_call/3]).   40
   41:- use_module(library(assertions)).   42:- use_module(library(metaprops)).   43:- use_module(library(neck)).   44:- use_module(library(rtchecks_flags)).   45:- use_module(library(send_check)).   46:- use_module(library(clambda)).   47:- use_module(library(ctrtchecks)).   48:- use_module(library(call_inoutex)).   49:- reexport(library(ctrtchecks),
   50            ['$with_asr'/2,
   51             '$with_gloc'/2,
   52             '$with_ploc'/2,
   53             check_call/3]).   54:- init_expansors.

Predicates that are required to implement run-time checks

Algorithm:

pred :- body.

is executed as if where transformed to:

   96:- thread_local rtchecks_disabled/0.   97
   98rtchecks_disable :- assertz(rtchecks_disabled).
   99
  100rtchecks_enable :- retractall(rtchecks_disabled).
  101
  102:- meta_predicate '$with_ploc'(0 ).  103
  104'$with_ploc'(Goal) :-
  105    ( prolog_current_frame(Curr),
  106      prolog_frame_attribute(Curr,  parent, Frame),
  107      prolog_frame_attribute(Frame, clause, Clause),
  108      prolog_frame_attribute(Curr,  pc,     PC)
  109    ->'$with_ploc'(Goal, clause_pc(Clause, PC))
  110    ; Goal
  111    ).
  112
  113:- meta_predicate rtcheck_pred(0, +, +, +).  114
  115rtcheck_pred(CM:Goal, M, CM, RAsrL) :-
  116    '$with_ploc'(do_rtcheck_pred(CM:Goal, M, CM, RAsrL)).
  117
  118do_rtcheck_pred(CM:Goal, M, CM, RAsrL) :-
  119    ( rtchecks_disabled
  120    ->CM:Goal
  121    ; call_inoutex(
  122          check_goal(rt,
  123                     call_inoutex(CM:Goal,
  124                                  rtchecks_enable,
  125                                  rtchecks_disable),
  126                     M, CM, RAsrL),
  127          rtchecks_disable,
  128          rtchecks_enable)
  129    ).
  130
  131assertions:asr_aprop(rtcheck(Asr), Key, Prop, From) :-
  132    curr_prop_asr(Key, Prop, From, Asr).
  133
  134% ----------------------------------------------------------------------------
  135
  136:- meta_predicate do_rtcheck(+, 0, +).  137
  138status_cond(false,  Call) :- Call.
  139status_cond(Status, Call) :-
  140    rtcheck_assr_status(Status),
  141    Status \= false,
  142    neck,
  143    \+ Call.
  144
  145do_rtcheck(Status, Call, PLoc) :-
  146    '$with_ploc'(status_cond(Status, Call), PLoc),
  147    last_prop_failure(L),
  148    send_check([[]/Call-L], pp_check, Status/1, PLoc, []),
  149    fail.
  150
  151:- meta_predicate rtcheck_call(+,0,+).  152
  153rtcheck_call(Status, Call, PLoc) :-
  154    setup_call_cleanup(
  155        rtchecks_disable,
  156        \+ do_rtcheck(Status, Call, PLoc),
  157        rtchecks_enable)