6
$\begingroup$

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?

$\endgroup$
2
  • 3
    $\begingroup$ Many functions in the base language are actually implemented in a lower-level, faster language and highly optimized for speed. Users have no access to the implementation of those functions. There are, however, many built-in higher-level functions that are written in the Wolfram language, whose definitions you can sometimes find using spelunking tools (see What is the most convenient way to read definitions of in-memory symbols when we don't have the source files? (Spelunking tools)). $\endgroup$
    – MarcoB
    Commented Jan 28, 2016 at 8:21
  • 1
    $\begingroup$ I have marked this question as a duplicate. (See the link inserted at the top.) Please read the original and its answers. If you feel that your intended question is not addressed there please edit this post to specifically note how your query is different. $\endgroup$
    – Mr.Wizard
    Commented Jan 28, 2016 at 8:28

1 Answer 1

5
$\begingroup$

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
*)
$\endgroup$

Not the answer you're looking for? Browse other questions tagged or ask your own question.