Skip to content

Commit f73b831

Browse files
committed
Added KernelTunnels
1 parent 0e4da82 commit f73b831

File tree

3 files changed

+1312
-177
lines changed

3 files changed

+1312
-177
lines changed

InterfaceObjects.m

+201-6
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
(* ::Package:: *)
22

3-
3+
(* Autogenerated Package *)
44

55
(* ::Section:: *)
66
(*Interfaces*)
@@ -39,6 +39,8 @@
3939
"Alias for defining object methods";
4040
InterfaceAttribute::usage=
4141
"Alias for defining object attributes";
42+
InterfaceOverride::usage=
43+
"Alias for defining object UpValues";
4244

4345

4446
Begin["`Private`"];
@@ -314,12 +316,13 @@
314316
"Constructor"->Automatic,
315317
"MutationHandler"->Automatic,
316318
"MutationFunctions"->None,
319+
"Protect"->False,
317320
"AccessorFunctions"->Automatic,
318321
"NormalFunction"->Automatic,
319322
"Formatted"->True,
320323
"Icon"->None,
321324
"DefaultMethods"->{
322-
Keys, SetProperty, PropertyValue,
325+
Association, Keys, Values, SetProperty, PropertyValue,
323326
RemoveProperty, PropertyList
324327
},
325328
"DefaultAttributes"->{
@@ -416,6 +419,7 @@
416419
];
417420
iRegisterDefaultInterfaceMethods[head, OptionValue["DefaultMethods"]];
418421
iRegisterDefaultInterfaceAttributes[head, OptionValue["DefaultAttributes"]];
422+
If[OptionValue["Protect"], Protect[head]];
419423
head
420424
];
421425

@@ -601,6 +605,7 @@
601605
},
602606
InterfaceConstructor[head]=constructor;
603607
(* Constructor DownValue on the object *)
608+
Unprotect[head];
604609
head//ClearAll;
605610
head~SetAttributes~HoldAllComplete;
606611
head[args___]?checkInvalid:=
@@ -623,6 +628,102 @@
623628

624629

625630

631+
$noArgMutations=
632+
Alternatives@@
633+
{Unset, Increment, Decrement};
634+
635+
636+
$oneArgMutations=
637+
Alternatives@@
638+
{
639+
Set, SetDelayed,
640+
AddTo, SubtractFrom, TimesBy, DivideBy,
641+
AppendTo, PrependTo,
642+
AssociateTo, KeyDropFrom
643+
};
644+
645+
646+
iRegisterInterfaceMutationHandler[
647+
head_,
648+
func_,
649+
dispatcher_
650+
]:=
651+
With[
652+
{
653+
oQ=InterfaceValidator[head],
654+
symQ=Unique[symbolQ],
655+
oA=$oneArgMutations,
656+
nA=$noArgMutations,
657+
d=
658+
If[!AssociationQ@dispatcher,
659+
<|"Keys"->dispatcher|>,
660+
dispatcher
661+
]
662+
},
663+
symQ~SetAttributes~{Temporary, HoldFirst};
664+
symQ[sym_]:=
665+
MatchQ[OwnValues[sym], {_:>_head?oQ}];
666+
func // ClearAll;
667+
func~SetAttributes~HoldAllComplete;
668+
If[KeyExistsQ[d, "Self"],
669+
With[{subdispatch=dispatcher["Self"]},
670+
func[
671+
(h: oA)[obj_Symbol?symQ, val_]
672+
] :=
673+
With[{res=subdispatch[h][obj, val]},
674+
(obj=res)/;Head[res]===head
675+
];
676+
func[
677+
(h : nA)[obj_Symbol?symQ]
678+
] :=
679+
With[{res=subdispatch[h][obj]},
680+
(obj=res)/;Head[res]===head
681+
];
682+
];
683+
];
684+
If[KeyExistsQ[d, "Keys"],
685+
With[{subdispatch=dispatcher["Keys"]},
686+
func[
687+
(h: oA)[obj_Symbol?symQ[attr__], val_]
688+
] :=
689+
With[{res=subdispatch[h][obj, {attr}, val]},
690+
(obj=res)/;Head[res]===head
691+
];
692+
func[
693+
(h : nA)[obj_Symbol?symQ[attr__]]
694+
] :=
695+
With[{res=subdispatch[h][obj, {attr}]},
696+
(obj=res)/;Head[res]===head
697+
];
698+
]
699+
];
700+
If[KeyExistsQ[d, "Parts"],
701+
With[{subdispatch=dispatcher["Parts"]},
702+
func[
703+
(h: oA)[obj_Symbol?symQ[[part__]], val_]
704+
] :=
705+
With[{res=subdispatch[h][obj, {part}, val]},
706+
(obj=res)/;Head[res]===head
707+
];
708+
func[
709+
(h : nA)[obj_Symbol?symQ[[part__]]]
710+
] :=
711+
With[{res=subdispatch[h][obj, {part}]},
712+
(obj=res)/;Head[res]===head
713+
];
714+
]
715+
];
716+
(* fallthrough to get normal behavior back *)
717+
func[___] := Language`MutationFallthrough;
718+
Language`SetMutationHandler[head, func];
719+
]
720+
721+
722+
(* ::Subsubsection::Closed:: *)
723+
(*iRegisterInterfaceMutationHandler*)
724+
725+
726+
626727
$noArgMutations=
627728
Alternatives@@
628729
{Unset, Increment, Decrement};
@@ -731,8 +832,10 @@
731832
{
732833
mn=methodName,
733834
valid=InterfaceValidator[head],
734-
meths=InterfaceMethods[head]
835+
meths=InterfaceMethods[head],
836+
attrs=Attributes[head]
735837
},
838+
If[MemberQ[attrs, Protected], Unprotect[head]];
736839
InterfaceMethods[head]=
737840
If[meths===Null,
738841
<|mn->True|>,
@@ -742,6 +845,7 @@
742845
def;
743846
head/:lhs?valid[mn][args]:=
744847
def;
848+
If[MemberQ[attrs, Protected], Protect[head]];
745849
];
746850

747851

@@ -779,15 +883,18 @@
779883
{
780884
mn=methodName,
781885
valid=InterfaceValidator[head],
782-
meths=InterfaceAttributes[head]
886+
meths=InterfaceAttributes[head],
887+
attrs=Attributes[head]
783888
},
889+
If[MemberQ[attrs, Protected], Unprotect[head]];
784890
InterfaceAttributes[head]=
785891
If[meths===Null,
786892
<|mn->True|>,
787893
Append[meths, mn->True]
788894
];
789895
head/:lhs?valid[mn]:=
790896
def;
897+
If[MemberQ[attrs, Protected], Protect[head]];
791898
];
792899

793900

@@ -802,6 +909,70 @@
802909
Protect[InterfaceAttribute]
803910

804911

912+
(* ::Subsubsection::Closed:: *)
913+
(*iRegisterInterfaceOverride*)
914+
915+
916+
917+
iRegisterInterfaceOverride~SetAttributes~HoldRest;
918+
iRegisterInterfaceOverride[
919+
head_,
920+
upval_,
921+
{lh___},
922+
main_,
923+
{rh___},
924+
def_
925+
]:=
926+
With[
927+
{
928+
mn=upval,
929+
valid=InterfaceValidator[head],
930+
attrs=Attributes[head]
931+
},
932+
If[MemberQ[attrs, Protected], Unprotect[head]];
933+
head/:upval[lh, main?valid, rh]:=
934+
def;
935+
If[MemberQ[attrs, Protected], Protect[head]];
936+
];
937+
938+
939+
Unprotect[InterfaceOverride];
940+
InterfaceOverride/:
941+
(
942+
InterfaceOverride[head_][
943+
upval_[args___]
944+
]:=def_
945+
):=
946+
Module[
947+
{
948+
pivot,
949+
argList
950+
},
951+
argList=Thread[HoldComplete[{args}]];
952+
pivot=
953+
SelectFirst[Range[Length[argList]], !FreeQ[argList[[#]], head]&,
954+
Length[argList]+1];
955+
argList=
956+
Map[
957+
Thread[#, HoldComplete]&,
958+
{
959+
Take[argList, UpTo[pivot-1]],
960+
argList[[{pivot}]],
961+
Drop[argList, UpTo[pivot]]
962+
}
963+
];
964+
Replace[argList,
965+
{
966+
HoldComplete[{lh___}]|{lh___},
967+
HoldComplete[{main_}],
968+
HoldComplete[{rh___}]|{rh___}
969+
}:>
970+
iRegisterInterfaceOverride[head, upval, {lh}, main, {rh}, def]
971+
]
972+
];
973+
Protect[InterfaceOverride]
974+
975+
805976
(* ::Subsubsection::Closed:: *)
806977
(*iRegisterInterfaceAccessor*)
807978

@@ -815,9 +986,13 @@
815986
},
816987
If[KeyExistsQ[ea, "Keys"],
817988
With[{lookup=dispatcher["Keys"]},
818-
obj_head?valid[attr_String?(!KeyExistsQ[InterfaceMethods[head], #]&)]:=
989+
obj_head?valid[attr_String?(
990+
!KeyExistsQ[
991+
Join[InterfaceMethods[head], InterfaceAttributes[head]],
992+
#
993+
]&)]:=
819994
With[{res=lookup[obj, attr]}, res/;Head[res]=!=lookup];
820-
obj_head?valid[attr:Except[_String|_String[]]]:=
995+
obj_head?valid[attr:Except[_String|_String[___]]]:=
821996
With[{res=lookup[obj, attr]}, res/;Head[res]=!=lookup];
822997
obj_head?valid[attr1_, attrs__]:=
823998
With[{res=lookup[obj, attr1, attrs]}, res/;Head[res]=!=lookup];
@@ -934,6 +1109,16 @@
9341109

9351110

9361111

1112+
(* ::Subsubsubsection::Closed:: *)
1113+
(*Association*)
1114+
1115+
1116+
1117+
registerDefaultMethod[head_, validator_, Association]:=
1118+
head/:HoldPattern@Association[head[a_]?validator]:=
1119+
Association[a];
1120+
1121+
9371122
(* ::Subsubsubsection::Closed:: *)
9381123
(*Keys*)
9391124

@@ -944,6 +1129,16 @@
9441129
Keys[a];
9451130

9461131

1132+
(* ::Subsubsubsection::Closed:: *)
1133+
(*Values*)
1134+
1135+
1136+
1137+
registerDefaultMethod[head_, validator_, Values]:=
1138+
head/:HoldPattern@Values[head[a_]?validator]:=
1139+
Values[a];
1140+
1141+
9471142
(* ::Subsubsubsection::Closed:: *)
9481143
(*ReplacePart*)
9491144

0 commit comments

Comments
 (0)