-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontext.pl
More file actions
988 lines (754 loc) · 28.2 KB
/
context.pl
File metadata and controls
988 lines (754 loc) · 28.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
/*
Author: Pieter Van den Abeele
E-mail: pvdabeel@mac.com
Copyright (c) 2005-2026, Pieter Van den Abeele
Distributed under the terms of the LICENSE file in the root directory of this
project.
*/
/** <module> CONTEXT
Context is an object-oriented programming paradigm for Prolog. It implements
contexts (namespaces), classes and instances. It supports various inheritance
mechanisms. Access to member predicates is regulated through public/protected
& private meta-predicates. We enable declarative static typing of class data
members.
In short, a clever mechanism to enable:
- Working with more than one person on the same application
- Better readability of code
- Better conrol of dynamic facts and rules
- Better performance by evaluating rules in the right context
The syntax is comparable to LogTalk, but LogTalk takes a compilation approach,
translating to regular prolog. This implementation works at runtime by
dynamically generating the necessary predicates.
-------------------------------------------------------------------------------
* CONTEXT - Contextual Object Oriented Logic Programming
-------------------------------------------------------------------------------
CONTEXT implements a declarative contextual logic programming paradigm
that aims to facilitate Prolog software engineering.
Short description:
1. We split up the global Prolog namespace into contexts, each having
their own facts and rules.
2. We create a meta-language allowing you to declare metadata about
facts and rules in a context.
3. We implement Classes and Instances, Public, Protected and Private
meta-predicates. We implement (Multiple) inheritance and cloning.
We implement operators enabling interaction with context.
Programs written using CONTEXT are much smaller, support team development
and can still be converted easily to traditional prolog code.
-------------------------------------------------------------------------------
* Long description:
-------------------------------------------------------------------------------
A context groups together clauses of a prolog application. By default,
clauses are local to their context i.e. not seen by other contexts, unless
properly prefixed with the name of their context. An exception to this rule
are clauses corresponding to predicates declared as "exported".
Context rules are evaluated in the context in which they are defined. An
exception to this rule are clauses declared as "transparent", which inherit
their context from the predicate that is calling them.
Note that contexts are created ex nihilo. Referencing them is enough to
create one.
The contextual logic programming paradigm as implemented by CONTEXT, adds a
number of features that stem from Object Oriented programming. Classes and
their instances are contexts.
A class is a special type of context which declares public, protected, and
private meta-predicates. Predicate properties are interpreted differently
for each of the following actions:
- instantiation
- inheritance
- calling a predicate
Instances are dynamically created from a class. Predicates declared by the
corresponding class as private, public or protected are "guarded"
correspondingly in the instance context that is dynamically created and
populated with the necessary guarded predicates in order to prevent
unauthorized access.
An instance enables data-member-like behaviour. This functionality is achieved
through the use of special operators. These operators allow one to cache a
successful evaluation of a unified context predicate.
This implementation is thread-safe and supports serialization.
-------------------------------------------------------------------------------
* Examples:
-------------------------------------------------------------------------------
@see examples/person.pl for an example 'person' class.
*/
% =============================================================================
% CONTEXT declarations
% =============================================================================
% -----------------------------------------------------------------------------
% Exported predicates and operators
% -----------------------------------------------------------------------------
% CONTEXT exports a number of predicates and operator declarations.
:- module(context, [ class/0,
class/1,
newinstance/1,
dpublic/1,
dprotected/1,
dprivate/1,
ddynamic/1,
dstatic/1,
dmutex/1,
declare/1,
declared/1,
implemented/1,
instances/2,
inherit/1,
this/1,
(~)/1,
(:)/1,
(::)/1,
(<-)/1,
(<=)/1,
(<+)/1,
(::)/2,
(<-)/2,
(<=)/2,
(<+)/2,
(://)/2,
op(600, fx, '~'),
op(600, fx, ':'),
op(600, fx, '::'),
op(600, fx, '<-'),
op(600, fx, '<='),
op(600, fx, '<+'),
op(601, xfx, '::'),
op(600, xfx, '<-'),
op(600, xfx, '<='),
op(600, xfx, '<+'),
op(601, xfx, ':'),
op(602, xfx, '?'),
op(603, xfx, '://'),
op(1200, xfx, '::-')
]).
% -----------------------------------------------------------------------------
% Transparent predicates and operators
% -----------------------------------------------------------------------------
% CONTEXT declares a number of rules as module transparent. The
% clauses corresponding with these predicates require access to the context
% in which they are used instead of the context in which they are declared.
:- module_transparent class/0,
class/1,
newinstance/1,
dpublic/1,
dprotected/1,
dprivate/1,
ddynamic/1,
dstatic/1,
dmutex/1,
declare/1,
declared/1,
implemented/1,
inherit/1,
this/1,
% no destructor since destructor cannot be called directly.
% e.g. ~pieter(foo). vs pieter:'~'(foo)
(:)/1,
(::)/1,
(<-)/1,
(<=)/1,
(<+)/1,
(::)/2,
(<-)/2,
(<=)/2,
(<+)/2,
(://)/2.
% -----------------------------------------------------------------------------
% Thread-awareness
% -----------------------------------------------------------------------------
% CONTEXT is thread-aware. The idea is that a context, class or instance can be
% used by different threads at the same time. Tokens are issued by a class to
% verify access and invocation methods. They need to be local to the thread
% using the class, otherwise access could be granted to a thread which should
% not have had access to a particular clause.
:- thread_local '$_token'/1.
% -----------------------------------------------------------------------------
% Dynamic meta predicates
% -----------------------------------------------------------------------------
% CONTEXT uses a dynamic predicate called meta.
:- dynamic '$__meta'/1.
% -----------------------------------------------------------------------------
% Operator declarations
% -----------------------------------------------------------------------------
% CONTEXT operator declarations.
:- op(600, fx, '~').
:- op(600, fx, ':').
:- op(600, fx, '<-').
:- op(600, fx, '<=').
:- op(600, fx, '<+').
:- op(601, xfx, '::').
:- op(600, xfx, '<-').
:- op(600, xfx, '<=').
:- op(600, xfx, '<+').
:- op(601, xfx, ':').
:- op(602, xfx, '?').
:- op(603, xfx, '://').
:- op(1200, xfx, '::-').
% -----------------------------------------------------------------------------
% Exported predicates (this, class)
% -----------------------------------------------------------------------------
%! this(?Context)
%
% Exported, transparant predicate
%
% Retrieves the current context.
this(Context) :-
context_module(Context).
%! class
%
% Exported, transparent predicate
%
% Declare a class.
class :-
class([]).
%! class(+Parents)
%
% Exported, transparent predicate
%
% Declare a class.
class(Parents) :-
this(Context),
context:declare(Context,type(class)),
inherit(Parents).
% -----------------------------------------------------------------------------
% Exported predicates (dpublic, dprotected, dprivate, ddyn, dstatic, dmutex)
% -----------------------------------------------------------------------------
%! dpublic(+Functor/+Arity)
%
% Exported, transparent predicate
%
% Declare a public predicate.
dpublic(Functor/Arity) :-
!,
declare(property(Functor/Arity, public)).
dpublic(List) :-
!,
maplist(dpublic,List).
%! dprotected(+Functor/+Arity)
%
% Exported, transparent predicate
%
% Declare a protected predicate.
dprotected(Functor/Arity) :-
!,
declare(property(Functor/Arity, protected)).
dprotected(List) :-
!,
maplist(dprotected,List).
%! dprivate(+Functor/+Arity)
%
% Exported, transparent predicate
%
% Declare a private predicate.
dprivate(Functor/Arity) :-
!,
declare(property(Functor/Arity, private)).
dprivate(List) :-
!,
maplist(dprivate,List).
%! ddynamic(+Functor/+Arity)
%
% Exported, transparent predicate
%
% Declare a dynamic predicate.
ddynamic(_Functor/_Arity) :-
!,
true.
ddynamic(List) :-
!,
maplist(ddynamic,List).
%! ddynamic(+Functor/+Arity)
%
% Exported, transparent predicate
%
% Declare a dynamic predicate.
dstatic(Functor/Arity) :-
!,
declare(property(Functor/Arity, static)).
dstatic(List) :-
!,
maplist(dstatic,List).
%! dmutex(+Functor/+Arity)
%
% Exported, transparent predicate
%
% Declare a mutexed predicate.
dmutex(Functor/Arity) :-
!,
declare(property(Functor/Arity, mutex)).
dmutex(List) :-
!,
maplist(dmutex,List).
% -----------------------------------------------------------------------------
% Exported predicates (inheritance)
% -----------------------------------------------------------------------------
%! inherit(+Parents)
%
% Exported, transparent predicate
%
% Inherit predicates from a number of parent contexts.
% Throws an error if parent context does not exist.
inherit([Parent|Parents]) :-
inherit(Parent),
inherit(Parents).
inherit([]) :- !.
%! inherit(+Parent)
%
% Exported, transparent predicate
%
% Inherit predicates from a parent context.
% Throws an error if parent context does not exist.
inherit(Parent) :-
\+(Parent:declared(type(class))),
this(Context),
throw(error(existence_error(class, Parent), context(Context, inherit))).
inherit(Parent) :-
findall(Functor/Arity, Parent:declared(property(Functor/Arity,_)), List),
this(Context),
Context:declare(parent(Parent)),
context:inherit_predicates(class, Context, Parent, List).
% -----------------------------------------------------------------------------
% Exported predicates (newinstance)
% -----------------------------------------------------------------------------
%! newinstance(+class)
%
% Exported, transparent predicate
%
% Create an instance from a class.
newinstance(Class) :-
this(Context),
context:newinstance(Context, Class).
% -----------------------------------------------------------------------------
% Exported predicates (declare, declared, implemented)
% -----------------------------------------------------------------------------
%! declare(+Fact)
%
% Exported, transparent
%
% Declare a fact.
declare(Fact) :-
this(Context),
context:declare(Context, Fact).
%! declared(?Fact)
%
% Exported, transparent predicate
%
% Check whether a fact is declared.
declared(Fact) :-
this(Context),
clause(Context:'$__meta'(Fact), true).
%! implemented(+Functor/+Arity)
%
% Exported, transparent predicate
%
% Check whether a fact is implemented.
implemented(Functor/Arity) :-
this(Context),
functor(Head,Functor,Arity),
Context:implemented(Head).
%! implemented(+Head)
%
% Exported, transparent predicate
%
% Check whether a fact is implemented.
implemented(Head) :-
this(Context),
context:translate_call(Head,GuardedHead),
clause(Context:GuardedHead,_).
% -----------------------------------------------------------------------------
% Exported predicates (freeze)
% -----------------------------------------------------------------------------
%! freeze(Context)
%
% Exported predicate
%
% Freezes the dynamically generated predicates in an instance to
% ensure optimal performance.
context:freeze(Context) :-
findall(Context:F/A,
( Context:declared(property(F/A,Prop)),
Prop \= static ),
DynList),
compile_predicates(DynList).
% -----------------------------------------------------------------------------
% Exported predicates (destroy)
% -----------------------------------------------------------------------------
%! '~'(+Destructor)
%
% Exported predicate
%
% Destroy context, calling destructor.
'~'(DestructorCall) :-
DestructorCall =.. [Context|_Arguments],
current_module(Context,_),
throw(error(permission_error(destroy, static_context, Context), context(destroy, _))).
'~'(DestructorCall) :-
DestructorCall =.. [Context|_Arguments],
Context:declared(type(class)),
context:destroy(Context).
'~'(DestructorCall) :-
DestructorCall =.. [Context|Arguments],
Context:declared(type(instance(Parent))),
length(Arguments, Arity),
atom_concat('~', Parent, DFunctor),
Destructor =.. [DFunctor|Arguments],
( Context:declared(property(DFunctor/Arity, _)) -> Context:Destructor ; true ),
!,
context:destroy(Context).
% -----------------------------------------------------------------------------
% Exported predicates (operator implementations)
% -----------------------------------------------------------------------------
%! ':'(+Predicate)
%
% Exported, transparent predicate
%
% Extension of (:)/2 as a reference to 'this', no-cache.
':'(Predicate) :-
this(Context),
Context:Predicate.
%! '::'(+Predicate)
%
% Exported, transparent predicate
%
% Extension of (::)/2 as a reference to 'this', cache-only.
'::'(Predicate) :-
this(Context),
Context::Predicate.
%! '<-'(+Predicate)
%
% Exported, transparent predicate
%
% Extension of <-/2 as a reference to 'this'.
'<-'(Predicate) :-
this(Context),
Context<-Predicate.
%! '<='(+Predicate)
%
% Exported, transparent predicate
%
% Extension of <=/2 as a reference to 'this'.
'<='(Predicate) :-
this(Context),
Context<=Predicate.
%! '<+'(+Predicate)
%
% Exported, transparent predicate
%
% Extension of <+/2 as a reference to 'this'.
'<+'(Predicate) :-
this(Context),
Context<+Predicate.
% '::'(+Context, +Predicate)
%
% Exported, transparent predicate
%
% Call a guarded context predicate.
'::'(Context, Predicate) :-
Context:declared(cache(Predicate)),
Context:Predicate.
%! '://'(+Context, +Predicate)
%
% Exported, transparent predicate
%
% Evaluate predicate within a given context
'://'(Context, Predicate) :-
Context:Predicate.
%! '<-'(+Context, +Predicate)
%
% Exported, transparent predicate
%
% Retract predicate cache matching Predicate.
'<-'(Context, Predicate) :-
context:undeclare(Context, cache(Predicate)).
%! '<='(+Context, +Predicate)
%
% Exported, transparent predicate
%
% Assert predicate cache, destroying existing cache.
% Algorithm:
%
% -Use Functor/Arity to create query
% -Execute
% -Retract existing cache
% -Assert new cache
'<='(Context, Predicate) :-
functor(Predicate, Functor, Arity),
functor(Head, Functor, Arity),
Context:Predicate,
context:undeclare(Context, cache(Head)),
context:declare(Context, cache(Predicate)).
%! '<+'(+Context, +Predicate)
%
% Exported, transparent predicate
%
% Assert predicate cache.
% Algorithm:
%
% -Use Functor/Arity to create query
% -Execute
% -Assert new cache if not exists
'<+'(Context, Predicate) :-
Context:Predicate,
context:declare(Context, cache(Predicate)).
% -----------------------------------------------------------------------------
% Local helper predicates (newinstance)
% -----------------------------------------------------------------------------
%! newinstance(+class, +constructor)
%
% Local predicate
%
% Create an instance from a class.
context:newinstance(Context, _Constructor) :-
\+(atom(Context)),
throw(error(type_error(atom, Context), context(instance, _))).
context:newinstance(Context, Constructor) :-
Constructor =.. [Parent|_],
\+(Parent:declared(type(class))),
throw(error(existence_error(class, Parent), context(Context, instance))).
context:newinstance(Context, Constructor) :-
Constructor =.. [Parent|Arguments],
thread_local(Context:'$_token'/1),
context:declare(Context, type(instance(Parent))),
findall(Predicate, Parent:declared(property(Predicate,_)), List),
context:inherit_predicates(instance, Context, Parent, List),
length(Arguments, Arity),
freeze(Context),
!,
( Context:declared(property(Parent/Arity, _)) -> Context:Constructor ; true ).
% -----------------------------------------------------------------------------
% Local helper predicates (inheritance)
% -----------------------------------------------------------------------------
%! inherit_predicates(+Relation, +Context, +Parent, +List)
%
% Local predicate
%
% Inherits predicates in List from Parent Context.
context:inherit_predicates(Relation, Context, Parent, [Functor/Arity|Tail]) :-
context:inherit_predicate(Relation, Context, Parent, Functor/Arity),
context:inherit_predicates(Relation, Context, Parent, Tail).
context:inherit_predicates(_Relation, _Context, _Parent, []).
%! inherit_predicate(+Relation, +Context, +Parent, +Functor/+Arity)
%
% Local predicate
%
% Inherit predicate from parent context. Inherits the predicate properties,
% corresponding clause and cache.
context:inherit_predicate(Relation, Context, Parent, Functor/Arity) :-
context:inherit_predicate_property(Relation, Context, Parent, Functor/Arity),
context:inherit_predicate_clauses(Relation, Context, Parent, Functor/Arity).
%! inherit_predicate_property(+Relation, +Context, +Parent, +Functor/Arity)
%
% Local predicate
%
% Assert property of inherited predicate.
context:inherit_predicate_property(Relation, Context, Parent, Functor/Arity) :-
Parent:declared(property(Functor/Arity, Property)),
context:inherit_predicate_property(Relation, Property, Context, Parent, Functor/Arity).
%! inherit_predicate_property(+Relation, +Property, +Context, +Parent, +Predicate)
%
% Local predicate
%
% Assert property of inherited predicate, depending on relationship.
context:inherit_predicate_property(class, private, _Context, _Parent, _Predicate) :-
!,
true.
context:inherit_predicate_property(_Relation, Property, Context, _Parent, Predicate) :-
context:declare(Context, property(Predicate, Property)).
%! inherit_predicate_clauses(+Relation, +Context, +Parent, +Functor/+Arity)
%
% Local predicate
%
% Inherit clauses corresponding with Functor/Arity, guard them if required.
context:inherit_predicate_clauses(Relation, Context, Parent, Functor/Arity) :-
Parent:declared(property(Functor/Arity, Property)),
functor(Head, Functor, Arity),
context:inherit_predicate__metaclause(Relation, Parent, Context, Head, Functor/Arity),
findall([Head, Body], clause(Parent:'::-'(Head, Body), true), Clauses),
context:inherit_predicate_clauses(Relation, Property, Context, Parent, Clauses).
%! inherit_predicate__metaclause(+Relation, +Parent, +Context, +Head, +Functor/+Arity)
%
% Local predicate
%
% Create a guarded metaclause calling guarded clauses.
context:inherit_predicate__metaclause(class, _Parent, _Context, _Head, _Functor/_Arity) :-
!,
true.
context:inherit_predicate__metaclause(instance, Parent, Context, MetaHead, Functor/Arity) :-
Parent:declared(property(Functor/Arity,Property)),
context:gen_check_invocation(Context, Parent, Property, MetaHead, MetaBody),
context:assert_predicate_clause(instance, Context, MetaHead, MetaBody).
%! inherit_predicate_clauses(+Relation, +Property, +Context, +Parent, +Clauses)
%
% Local predicate
%
% Inherit clauses, guard them if required.
context:inherit_predicate_clauses(Relation, Property, Context, Parent, [[Head,Body]|Clauses]) :-
context:inherit_predicate_clause(Relation, Property, Context, Parent, Head, Body),
context:inherit_predicate_clauses(Relation, Property, Context, Parent, Clauses).
context:inherit_predicate_clauses(_Relation, _Property, _Context, _Parent, []).
%! inherit_preidcate_clause(+Relation, +Property, +Context, +Parent, +Head, +Body)
%
% Local predicate
%
% Rewrites clause body on inherit, depending on whether a class or an instance
% is inheriting a clause.
context:inherit_predicate_clause(class, private, _Context, _Parent, _Head, _Body) :-
!,
true.
context:inherit_predicate_clause(class, _Property, Context, _Parent, Head, Body) :-
context:assert_predicate_clause(class, Context, Head, Body).
context:inherit_predicate_clause(instance, Property, Context, Parent, MetaHead, Body) :-
context:guard_predicate_clause(Property, Context, Parent, MetaHead, GuardedHead, Body, GuardedBody),
context:assert_predicate_clause(instance, Context, GuardedHead, GuardedBody).
%! guard_predicate_clause(+Property, +Context, +Parent, +Head, +Body, -HeadNew, -BodyNew)
%
% Local predicate
%
% Given a clause, produces a guarded clause.
context:guard_predicate_clause(Property, Context, _Parent, Head, GuardedHead, Body, GuardedBody) :-
context:translate_call(Head, GuardedHead),
context:gen_check_access(Property, Context, Head, Body, GuardedBody).
% -----------------------------------------------------------------------------
% Local helper predicates (assert_predicate_clause)
% -----------------------------------------------------------------------------
%! assert_predicate_clause(+Relation, +Context, +Head, +Body)
%
% Local predicate
%
% Assert predicate clause
context:assert_predicate_clause(instance, Context, Head, Body) :-
expand_goal(Body,ExpandedBody),
assertz(Context:(Head :- ExpandedBody)).
context:assert_predicate_clause(class, Context, Head, Body) :-
assertz(Context:(Head ::- Body)).
% -----------------------------------------------------------------------------
% Local helper predicates (code generation: predicate guarding, access check)
% -----------------------------------------------------------------------------
%! gen_check_invocation(+Context, +Property, +Head, -Code)
%
% Local predicate
%
% Code generator for invocation check.
context:gen_check_invocation(Context, _Parent, public, MetaHead, Code) :-
context:translate_call(MetaHead,Head),
Code = (
call_cleanup(Head, retract(Context:'$_token'(thread_access)))
).
context:gen_check_invocation(_Context, _Parent, protected, MetaHead, Code) :-
context:translate_call(MetaHead,Head),
Code = (
Head
).
context:gen_check_invocation(_Context, _Parent, private, MetaHead, Code) :-
context:translate_call(MetaHead,Head),
Code = (
Head
).
context:gen_check_invocation(_Context, Parent, static, Head, Code) :-
Code = (
Parent:Head
).
%! gen_check_access(+Property, +Context, +Head, -Code)
%
% Local predicate
%
% Code generator for access check.
context:gen_check_access(private, Context, Head, Body, Code) :-
Code = (
clause(Context:'$_token'(thread_access), true)
-> ( % assertz(Context:'$_token'(thread_access)),
% reasoning: only public functions can call protected
% or private functions
Body )
; ( functor(Head,H,A),
throw(error(permission_error(access, private, H/A), context(Context, _))) )
).
context:gen_check_access(protected, Context, Head, Body, Code) :-
Code = (
clause(Context:'$_token'(thread_access), true)
-> ( % assertz(Context:'$_token'(thread_access)),
% reasoning: only public functions can call protected
% or private functions
Body )
; ( functor(Head,H,A),
throw(error(permission_error(access, protected, H/A), context(Context, _))) )
).
context:gen_check_access(static, _Context, _Head, Body, Code) :-
Code = (
Body
).
context:gen_check_access(_Property, Context, _Head, Body, Code) :-
Code = (
assertz(Context:'$_token'(thread_access)),
Body
).
% -----------------------------------------------------------------------------
% Local helper predicates (code generation: translate_call)
% -----------------------------------------------------------------------------
%! translate_call(?MetaCall, ?Call)
%
% Local predicate
%
% Translate between call and metacall representation.
context:translate_call(MetaCall, Call) :-
MetaCall =.. [MetaFunctor|Args],
Call =.. ['$__meta',guarded_implementation(MetaFunctor,Args)].
% -----------------------------------------------------------------------------
% Local helper predicates (destroy)
% -----------------------------------------------------------------------------
%! destroy(+Context)
%
% Local predicate
%
% Destroy a context.
context:destroy(Context) :-
findall(Functor/Arity, current_predicate(Context:Functor/Arity), List),
context:destroy_context_clauses(Context, List).
%! destroy_context_clauses(+Context, +List)
%
% Local predicate
%
% Destroy a list of contextual clauses
context:destroy_context_clauses(Context, [Functor/Arity|Preds]) :-
functor(Head, Functor, Arity),
predicate_property(Context:Head,dynamic),!,
ignore(catch(abolish(Context:Functor/Arity), _, true)),
context:destroy_context_clauses(Context, Preds).
context:destroy_context_clauses(Context, [_Functor/_Arity|Preds]) :-
!,
context:destroy_context_clauses(Context, Preds).
context:destroy_context_clauses(_, []) :- !.
% -----------------------------------------------------------------------------
% Helper predicates (declare, undeclare, conflicting)
% -----------------------------------------------------------------------------
%! context:declare(+Context, +Fact)
%
% Local predicate
%
% Declare a fact.
context:declare(Context, Fact) :-
assertz(Context:'$__meta'(Fact)).
%! context:undeclare(+Context, +Fact)
%
% Local predicate
%
% Undeclares a fact.
context:undeclare(Context, Fact) :-
retractall(Context:'$__meta'(Fact)).
%! context:conflicting(+Fact,-OtherFact)
%
% Local predicate
%
% Declares a conflict between facts:
%
% - A context can only be of one type.
% - A predicate can only have one property.
context:conflicting(type(_), type(_)) :- !.
context:conflicting(property(Functor/Arity, _), property(Functor/Arity, _)) :- !.
% -----------------------------------------------------------------------------
% Exported predicates (instances)
% -----------------------------------------------------------------------------
%! instances(+Class,?Instance)
%
% Exported predicate
%
% List instances of a class.
instances(Class,Instance) :-
current_module(Instance),
clause(Instance:'$__meta'(type(instance(Class))),true).