Search code examples
architectureprogramming-languagesdata-modelingabstract-data-type

Examples of Self-aware data and/or Self-aware patterns in programming languages


I am working on writing Self-aware resources for a larger data architecture project, and some specific targets are self aware components/resources for webpages.

(NOTE: I think self-aware data and functions are an important concept moving beyond event-based architectures, and behavioral programming in general to approach true AI functionality. Anyone with a 1500 reputation can apparently create a tag for "self-aware" which I cannot as this page indicated. Your help with that is appreciated if you like the direction of this post...thx)

I have a string "10" (lets call it x) there are many manipulations that can be done in many different langs. I can ctype_digit($x), or I can is_numeric($x) in PHP. But in order to develop self aware components, functions, and data architectures it would help to have x say "I am a string of the value '10'", or at the very least "I am a string". In a perfect world there would be a means to approach this from a binary level.

This reminds me of the difference between http and ftp. The difference is not necessarily the data types that are transferred between client and server. The difference is one transports and the other allows execution. Well, yes FTP does allow execution of a file, but allow me this distinction to explain one example of a difference between simply saying "oh this is a string because I said so at some earlier point."

In other words, it's not satisfactory to assign a data type and read the data type. That's not a self-aware/self-expressive data type. That's something that easy to read by convention.

At this point, any one example of a system (language dependent or otherwise) that can determine or complete the self-aware/self-expressive data type goal is an effective answer to the question (and much appreciated for alternate perspective!)

Thanks in advance.


Solution

  • We used to do this all the time in Smalltalk. In Smalltalk the mantra was "everything is an object" because Smalltalk does not expose primitives or explicit unboxing the way Java does. Go to http://www.compileonline.com/execute_smalltalk_online.php and try this:

    Transcript show: 10 class printString
    

    Like magic, the answer is:

      SmallInteger
    

    The number 10 is really an object that understands the message "class". In response to the message "class", 10 returns a the object that defines its behavior (i.e. its class). If we ask that class object for its name, it responds "SmallInteger".

    Try this:

    Transcript show: 'I am a ', 10 class printString, ' of value ', 10 printString
    
      I am a SmallInteger of value 10
    

    In fact, I could add a method whoAmI" to the class Object:

    whoAmI
    
       ^'I am a ', self class printString, ' of value ', self printString
    

    Once I did that, then:

    Transcript show: 3.34 whoAmI 
    
      I am a FloatD of value 3.34
    

    and even cooler:

    Transcript show: 3/4
    
      I am a Fraction of value 3/4
    

    (IMHO) well implemented object-oriented languages usually support this. C# is a mainstream OO language that can do some of this. Go here (http://www.compileonline.com/compile_csharp_online.php) and try this:

    using System.IO;
    using System;
    
    class Program
    { 
       public static void Main()
       {
    
          System.Console.WriteLine(10.ToString());
          System.Console.WriteLine(10.GetType().Name);
    
       }
    }
    

    In general, these kind of self-aware tricks are done in functional programming languages. Smalltalk is primarily an OO language, but it takes some inspiration from functional programming. If this kind of thing is really appealing to you, look into LISP or another functional language.