6

How can I look at the code of built-in functions?

Is there something like this:

LookInsideFunction[SubsetQ]

RETURNS

SubsetQ[list1_,list2_]:= stuff... 

is it possible to know how these functions are implemented in the Global Rule Base?

MarcoB
  • 67,153
  • 18
  • 91
  • 189
Conor
  • 7,449
  • 1
  • 22
  • 46

1 Answers1

5

There isn't always a good way to do it. There is Simon's Spelunking function, which Szabolcs has put on github, but in this case it doesn't help much:

Spelunk[SubsetQ]

enter image description here

There is also Trace, which is a bit more helpful here,

Trace[SubsetQ[{1, 2, 3}, {3, 1}]]
(* {HoldForm[SubsetQ[{1, 2, 3}, {3, 1}]], 
  {HoldForm[Internal`ConditionalValueBody[SubsetQ[{1, 2, 3}, {3, 1}], 
     {GeneralUtilities`Predicates`PackagePrivate`CompoundQ[{1, 2, 3}], 
      GeneralUtilities`Predicates`PackagePrivate`CompoundQ[{3, 1}], 
      Head[{1, 2, 3}] === Head[{3, 1}]}, 
     {{"normal", 1, Internal`ConditionalValueLHS}, 
      {"normal", 2, Internal`ConditionalValueLHS}, {"heads", Head[{1, 2, 3}], 
       Head[{3, 1}], 1, 2}}, 
     Length[Complement[GeneralUtilities`Predicates`PackagePrivate`values[
         {3, 1}], GeneralUtilities`Predicates`PackagePrivate`values[
         {1, 2, 3}]]] === 0]], 
   {{{{HoldForm[GeneralUtilities`Predicates`PackagePrivate`values[{3, 1}]], 
       HoldForm[If[AssociationQ[{3, 1}], Values[{3, 1}], {3, 1}]], 
       {HoldForm[AssociationQ[{3, 1}]], HoldForm[False]}, 
       HoldForm[If[False, Values[{3, 1}], {3, 1}]], HoldForm[{3, 1}]}, 
      {HoldForm[GeneralUtilities`Predicates`PackagePrivate`values[{1, 2, 3}]], 
       HoldForm[If[AssociationQ[{1, 2, 3}], Values[{1, 2, 3}], {1, 2, 3}]], 
       {HoldForm[AssociationQ[{1, 2, 3}]], HoldForm[False]}, 
       HoldForm[If[False, Values[{1, 2, 3}], {1, 2, 3}]], HoldForm[{1, 2, 3}]}, 
      HoldForm[Complement[{3, 1}, {1, 2, 3}]], HoldForm[{}]}, 
     HoldForm[Length[{}]], HoldForm[0]}, HoldForm[0 === 0], HoldForm[True]}, 
   HoldForm[True]}, HoldForm[True]} *)

You might be best served by TracePrint in this case,

TracePrint[SubsetQ[{1, 2, 3}, {3, 1}]]
(*
 SubsetQ[{1,2,3},{3,1}]
  SubsetQ
  {1,2,3}
  {3,1}
  Internal`ConditionalValueBody[SubsetQ[{1,2,3},{3,1}],{GeneralUtilities`Predicates`PackagePrivate`CompoundQ[{1,2,3}],GeneralUtilities`Predicates`PackagePrivate`CompoundQ[{3,1}],Head[{1,2,3}]===Head[{3,1}]},{{normal,1,Internal`ConditionalValueLHS},{normal,2,Internal`ConditionalValueLHS},{heads,Head[{1,2,3}],Head[{3,1}],1,2}},Length[Complement[GeneralUtilities`Predicates`PackagePrivate`values[{3,1}],GeneralUtilities`Predicates`PackagePrivate`values[{1,2,3}]]]===0]
   Internal`ConditionalValueBody
   Length[Complement[GeneralUtilities`Predicates`PackagePrivate`values[{3,1}],GeneralUtilities`Predicates`PackagePrivate`values[{1,2,3}]]]===0
    SameQ
    Length[Complement[GeneralUtilities`Predicates`PackagePrivate`values[{3,1}],GeneralUtilities`Predicates`PackagePrivate`values[{1,2,3}]]]
     Length
     Complement[GeneralUtilities`Predicates`PackagePrivate`values[{3,1}],GeneralUtilities`Predicates`PackagePrivate`values[{1,2,3}]]
      Complement
      GeneralUtilities`Predicates`PackagePrivate`values[{3,1}]
       GeneralUtilities`Predicates`PackagePrivate`values
       {3,1}
      If[AssociationQ[{3,1}],Values[{3,1}],{3,1}]
       If
       AssociationQ[{3,1}]
        AssociationQ
        {3,1}
       False
      If[False,Values[{3,1}],{3,1}]
      {3,1}
      GeneralUtilities`Predicates`PackagePrivate`values[{1,2,3}]
       GeneralUtilities`Predicates`PackagePrivate`values
       {1,2,3}
      If[AssociationQ[{1,2,3}],Values[{1,2,3}],{1,2,3}]
       If
       AssociationQ[{1,2,3}]
        AssociationQ
        {1,2,3}
       False
      If[False,Values[{1,2,3}],{1,2,3}]
      {1,2,3}
     Complement[{3,1},{1,2,3}]
     {}
    Length[{}]
    0
    0
   0===0
   True
  True
 True
*)
Jason B.
  • 68,381
  • 3
  • 139
  • 286