home *** CD-ROM | disk | FTP | other *** search
/ Global Amiga Experience / globalamigaexperience.iso / compressed / development / heliosdemodisk3.dms / heliosdemodisk3.adf / Tutorials / ForthLanguage.doc next >
Encoding:
Text File  |  1994-12-18  |  35.6 KB  |  704 lines

  1.  
  2.      ******************************************************
  3.      FORTH as an example of threaded interpretive languages
  4.      ******************************************************
  5.  
  6.      The HeliOS language is a member of the family of computer languages
  7.      known as "threaded interpretive languages".
  8.  
  9.      The most well known of these languages is FORTH, and HeliOS contains
  10.      many features which it shares with FORTH.
  11.  
  12.      In fact, HeliOS was originally designed as a Super-FORTH system for
  13.      the Amiga, and although it has now developed many unique features,
  14.      most of the old standard FORTH functions are still implemented.
  15.  
  16.      All HeliOS users will benefit from a knowledge of FORTH, so we include
  17.      here a brief "History and Discussion" of this interesting language.
  18.  
  19.      *******************
  20.      FORTH and the Amiga
  21.      *******************
  22.  
  23.      This is a general discussion on "what FORTH is" and "why we should use
  24.      it on an Amiga", for those who are unfamiliar with the language.
  25.  
  26.      The Amiga is probably the ultimate "value for money" computer so far
  27.      produced for domestic use, and irrespective of price many of us feel
  28.      it to have a special character which sets it apart from all its
  29.      commercial rivals.  It is above all an enthusiast's machine, with
  30.      unique hardware capabilities, effective multi-tasking, and a lot of
  31.      thoughtfully evolved support system software.
  32.  
  33.      With such a multi-faceted machine as the Amiga there is always the
  34.      feeling that there is much untapped potential still to be explored.
  35.  
  36.      If we are really keen to use all the fascinating special features of
  37.      the Amiga we inevitably have a fair amount of learning to do.  Worse
  38.      still there is no common programming language/environment available
  39.      on the Amiga which allows the ordinary non-expert computer enthusiast
  40.      to experiment and learn the machine in an interactive way whilst also
  41.      supplying the necessary speed and power.
  42.  
  43.      BASIC is never going to allow you to exploit the Amiga to its full
  44.      capability, and C is hardly user-friendly or interactive or fast or
  45.      compact or.......
  46.  
  47.      What we need for creative Amiga programming is a comfortable fully
  48.      interactive language with efficient support for all aspects of Amiga
  49.      software functionality.
  50.  
  51.      FORTH based languages are all wonderfully interactive in use, which
  52.      makes them ideal for creative programming, so FORTH is certainly worth
  53.      exploring as potentially an ideal Amiga programming language in spite
  54.      of the usual claims that "C" is the only "serious" Amiga language.
  55.  
  56.      HeliOS is a FORTH based language which is very fast and efficient: it
  57.      is fully Amiga compatible because it has been totally dedicated to and
  58.      developed for the Amiga.
  59.  
  60.      *************************
  61.      A little history of FORTH
  62.      *************************
  63.  
  64.      Perhaps we can have a look at the pedigree of FORTH and the other
  65.      members of its computer language "family".
  66.  
  67.      In the late 1960s Charles Moore, a truly original and innovative
  68.      thinker, introduced a computer language which embodied a radically
  69.      new approach to software design.
  70.  
  71.      He called his new language FORTH, for a slightly picturesque reason:
  72.  
  73.      "I was working on an IBM 1130, a "third generation" computer. The
  74.      result seemed so powerful that I considered it a "fourth generation
  75.      computer language". I would have called it FOURTH, except that the
  76.      1130 permitted only five character identifiers. So FOURTH became
  77.      FORTH, a nicer play on words anyway."
  78.  
  79.      Moore was unhappy with the software tools available to him at that
  80.      time, which in many cases were the already outmoded products of early
  81.      computer development work.  The software industry seemed ripe for a
  82.      new outlook based on the experience gained in early years of explosive
  83.      commercial progress.
  84.  
  85.      Like all idealists Moore may have thought that the world of computing
  86.      would welcome a new and better approach, or perhaps he took pleasure
  87.      in pure creativity independent of any reliance on traditional methods.
  88.      Certainly he made no attempt to embody traditional practices in his
  89.      new language and undoubtedly this originality was a liability in what
  90.      is still a very conservative industry.
  91.  
  92.      Moore set out to achieve two broadly compatible aims.
  93.  
  94.      Firstly, he wanted to make communication with a computer more natural
  95.      and intuitively satisfying: he wanted to write better software faster
  96.      and more enjoyably.
  97.  
  98.      Secondly, he tried to satisfy the technical criteria of elegance,
  99.      speed, compactness, efficiency, and portability.
  100.  
  101.      Moore was a rare blend of enthusiast, perfectionist, and philosopher,
  102.      and sadly enough his FORTH language has largely remained the province
  103.      of precisely that kind of computer enthusiast.
  104.  
  105.      The computing industry is organised in a way which tends, inevitably,
  106.      to resist change and unfortunately FORTH never made a breakthrough into
  107.      general usage.  Amazingly enough the computer languages in general use
  108.      today suffer from the same ridiculous limitations as were apparent to
  109.      Charles Moore in the late 1960s; indeed the very same languages are
  110.      still in use fundamentally unchanged.  FORTH is still as unique today
  111.      as it was in 1970, still an "odd man out", and still very well worth
  112.      investigating if you are prepared to try something different.
  113.  
  114.      FORTH gained some credibility when it became the adopted language of
  115.      the scientific Astronomical community and has been widely used in
  116.      observatories for critical software systems used in the precise
  117.      tracking control of telescopes.
  118.  
  119.      Although FORTH is not a common programming tool on today's powerful
  120.      home computers, this is not because it is outmoded, but because the
  121.      computer "marketplace" is dominated by stale commercial conservatism.
  122.  
  123.      FORTH is still a contender on the fringe of the software industry,
  124.      especially among high performance software enthusiasts, and manages to
  125.      keep currently available implementations on most microcomputers.
  126.  
  127.      Unfortunately, as we shall see later, some of these "domestic" versions
  128.      of FORTH have hardly done justice to the true power of the language.
  129.  
  130.      FORTH is such an unusual but ultimately natural way of communicating
  131.      with a computer that those of us who advocate its use tend to become
  132.      extremely enthusiastic about it.  We also sometimes find it hard to
  133.      convey to others its subtleties and advantages. In Moore's own words
  134.      again:
  135.  
  136.       "FORTH provides a natural means of communication between man
  137.      and the smart machines he is surrounding himself with. This requires
  138.      that it share characteristics of human languages, including
  139.      compactness, versatility, and extensibility. I cannot imagine a better
  140.      language for writing programs, expressing algorithms, or understanding
  141.      computers."
  142.  
  143.      One of the prime features designed into FORTH by Moore was simplicity
  144.      and an excellent immediate interpretive mode of operation.  He always
  145.      sought simple and elegant solutions and had a strong intrinsic belief
  146.      in the value of being able to interact directly with the computer at
  147.      the keyboard.
  148.  
  149.      **********************************
  150.      Why is FORTH different and better?
  151.      **********************************
  152.  
  153.      We need a fast, efficient, interactive and humanly readable language
  154.      to help us tame the problems associated with using a complex machine
  155.      to the limits of its capabilities.
  156.  
  157.      FORTH allows easy one-to-one communication with the computer in real
  158.      time - it is wonderfully "interactive" and this means that it can
  159.      also be FUN to use!  You can PLAY with the computer at the keyboard
  160.      using FORTH with a freedom which you could never achieve using any
  161.      other programming language.
  162.  
  163.      When faced with the Amiga, which is not exactly a simple machine, one
  164.      sometimes feels in real need of a tool to simplify and facilitate
  165.      interactive communication, instead of relying on the tedious process
  166.      of using a compiled language such as "C".
  167.  
  168.      Certainly there would be many more creative and original software
  169.      innovations if it were made easier to experiment at the keyboard.
  170.  
  171.      Obviously a machine like the Amiga has so many possibilities that we
  172.      must in some way simplify and classify our aims.  We have to use the
  173.      very best methods available to us if we are to exploit its special
  174.      hardware to the full.  Certainly we need to experiment and "play"
  175.      if we are to learn the full potential of the computer, but it is
  176.      no use having easy to use "interactive" tools if they are ultimately
  177.      not also powerful enough to exploit all aspects of the computer.
  178.  
  179.      Many easy-to-use BASIC languages allow you to do some things quite
  180.      easily, but at the expense of speed and without the full freedom to
  181.      explore every aspect of the Amiga's capability.
  182.  
  183.      FORTH is much more powerful than BASIC, it is fully interactive as
  184.      well as being a compiler, it is capable of direct hardware control
  185.      as well as high level operating system programming, and it has all
  186.      the capabilities of both "C" and "Assembler" built into one system.
  187.  
  188.      Furthermore, FORTH is the most suitable computer language available
  189.      for learning and experimenting because it uses a very intuitive and
  190.      flexible linguistic approach to computer communication: using FORTH
  191.      you teach your computer to speak YOUR language!
  192.  
  193.      If we are looking for tool to enable us to experiment, to learn, and
  194.      above all to enjoy ourselves; perhaps FORTH may be an answer, and
  195.      anyway it is doubtful that any of us will end up feeling that "C" is
  196.      more fun.
  197.  
  198.  
  199.      ***************
  200.      Reputations....
  201.      ***************
  202.  
  203.      In spite of its reputation for compactness and speed, and in spite of
  204.      its cult status with those who have grown to appreciate its subtlety,
  205.      FORTH has retained an ill deserved reputation for incomprehensibility.
  206.      You may have read journalistic accounts of FORTH's peculiar and maybe
  207.      "difficult" style of source code but don't be put off by these ill
  208.      founded rumours - they are just simply not true!
  209.  
  210.      The fact that FORTH has been criticised over "legibility" is an amazing
  211.      state of affairs when you consider that this kind of accusation is
  212.      rarely, if ever, levelled at "C", which really is an extremely poorly
  213.      designed language in terms of source code readabilty.
  214.  
  215.      The problem is that FORTH is DIFFERENT and UNIQUE, and those familiar
  216.      with what have become "conventional" concepts find it hard to change
  217.      and appreciate what are actually rather superior characteristics.
  218.  
  219.      Many journalists are quick to dismiss anything which is different,
  220.      simply because they have little time to assess anything which requires
  221.      learning new concepts.  Obviously FORTH is VERY different from the
  222.      commonly encountered languages like "C" or BASIC and as a result it
  223.      is simply dismissed as a curiosity.
  224.  
  225.      FORTH has very often suffered from misleading and offputting publicity
  226.      concerning its use of reverse polish notation, but in reality a little
  227.      understanding of the underlying principles of FORTH programming soon
  228.      makes the whole system seem most natural and even obvious.
  229.  
  230.      Try FORTH without prejudice, and try to forget most of what you may
  231.      already know about BASIC and "C": FORTH is a whole new world!
  232.  
  233.      *****************
  234.      Past incarnations
  235.      *****************
  236.  
  237.      Perhaps you may have come across FORTH in one of its early incarnations
  238.      on the popular 8-bit micros, and it must be said here that many of these
  239.      primitive implementations did it little justice and were often virtually
  240.      unusable.
  241.  
  242.      Those brave enough to try FORTH on these systems have suffered at the
  243.      hands of some terrible implementations which turned FORTH into a very
  244.      frustrating experience.  These inadequte FORTH implementations made the
  245.      language practically unusable except as a mildly interesting curiosity.
  246.  
  247.      Many of the commercially available 8-bit FORTHS were quite unworthy of
  248.      the name, so if you have had experience with these unfortunate offerings
  249.      then you should at least be prepared to give FORTH another chance.
  250.  
  251.      **************
  252.      FORTH concepts
  253.      **************
  254.  
  255.      FORTH is user-extensible computer language which is super-fast and
  256.      very memory efficient, easily customisable to individual taste, and
  257.      above all simple and fun to use.  The concepts and ideas behind FORTH
  258.      are outstandingly well thought out and it remains to this day a truly
  259.      excellent software tool if rightly used: FORTH is full of innovative
  260.      and powerful new techniques.
  261.  
  262.      If you take time off to study the operation of the ingenious FORTH
  263.      interpreter, you will see how FORTH threads together fast specialised
  264.      machine code routines to produce a high level interactive language
  265.      with a minimum of "overhead".  FORTH allows you to associate human
  266.      language words with computer routines in a simple and direct way.
  267.  
  268.      You will also see how FORTH code is not only very nearly as fast as
  269.      "pure" machine code, but is, incredibly enough, actually more compact
  270.      in many cases than a corresponding assembly language program.
  271.  
  272.      It is worth looking at the unique features of FORTH which allow us to
  273.      customise the language itself to our own requirements: using FORTH as
  274.      a basis you can literally build our own required new features into a
  275.      really usable and powerful programming toolkit.
  276.  
  277.      The real power of FORTH lies in this extendable construction and the
  278.      ease with which high level FORTH code can be translated into machine
  279.      code if required.
  280.  
  281.      FORTH is subtle but not over-complicated in the inner details of its
  282.      implementation and this means that it is possible to learn easily how
  283.      to ideally maximise the efficiency of your code.  To use FORTH really
  284.      productively it is well worth learning how the language works at the
  285.      innermost level, and this will be well within the capabilities of most
  286.      enthusiasts.
  287.  
  288.      FORTH allows the user get to grips with its inner workings in a way
  289.      which is impossible with other languages, and this gives a freedom to
  290.      FORTH programmers to use very powerful and ingenious methods usually
  291.      not available in languages other than assembler.
  292.  
  293.      Knowledge is power!
  294.  
  295.      ******************************
  296.      FORTH's unique use of language
  297.      ******************************
  298.  
  299.      The basic component of FORTH programming is the "word".
  300.  
  301.      A FORTH word is a coded function with a name which, when passed to
  302.      the interpreter, will generate a particular software command.
  303.  
  304.      Each word can either consist of a pure machine language routine or be
  305.      built up from a sequence of other previously defined FORTH words.
  306.  
  307.      FORTH programming consists largely of the process of defining, refining,
  308.      and combining words into ever more complex and powerful programs.
  309.  
  310.      FORTH is by its very nature extensible and malleable, and it has been
  311.      justly said that programming in FORTH consists in remodelling the very
  312.      language itself.
  313.  
  314.      Writing FORTH programs is really a matter of building a communication
  315.      channel between you and the computer, and in the process you actually
  316.      "teach" the machine to respond to "words" of your choice.
  317.  
  318.      One interesting result of the way FORTH works is that the language
  319.      can itself be built up in many different ways: all FORTH systems are
  320.      NOT equal!
  321.  
  322.      If we want an ideal FORTH system modelled especially for use on the
  323.      Amiga, we must concentrate on those factors which promote superior
  324.      performance in terms of speed, compactness, versatility, and creative
  325.      useability.  Above all we want to fully exploit all the features of
  326.      the Amiga without any restrictions imposed by the language itself.
  327.  
  328.      Let us explore generally the basic nature of FORTH and the essential
  329.      features which distinguish FORTH systems of differing capabilities.
  330.  
  331.      Like all computer languages FORTH comprises a system for translation
  332.      of human language - words - into the numerical symbolic language which
  333.      the CPU of the computer employs.
  334.  
  335.      FORTH optimises the human language side of the equation more than any
  336.      other computer language and all aspects of FORTH programming revolve
  337.      around the definition and use of words - human language words which
  338.      each user can choose and modify as desired.
  339.  
  340.      You can literally make FORTH source code read like English sentences,
  341.      choosing your own words for every software function and your own style
  342.      of parameter passing.
  343.  
  344.      A good FORTH programmer hardly needs many source code comments because
  345.      the language itself can be virtually as clear and explicit as written
  346.      English - at least to his/her self! (well - theoretically anyway!)
  347.  
  348.      As we shall see later this ideal state of lucidity is not so easy to
  349.      achieve in "standard" FORTH, so we need to construct an enhanced FORTH
  350.      vocabulary which makes code not only much more readable but also much
  351.      more efficient in terms of both compactness and speed.
  352.  
  353.      The size and quality of the set of pre-defined words which you get in
  354.      any FORTH system determines the ability of that system crucially.
  355.  
  356.      We need to avoid where possible having to use words which contribute
  357.      nothing to the logical task in hand; in particular we must try to avoid
  358.      strings of words where one word will suffice.
  359.  
  360.      Think of your FORTH system as an "alien" who you need to teach how to
  361.      help you: if the version of FORTH you use has an excellent vocabulary
  362.      already you will need to spend less time struggling to express yourself.
  363.  
  364.      It is interesting to note that the natural sequence of human thought,
  365.      and its expression in language, tends to come up with very well
  366.      structured formalisations if left to express itself freely.  We all
  367.      think differently, but we all think efficiently in our own way: we
  368.      need a freedom to express our own ideas in OUR way rather than being
  369.      restricted by someone else's preconceived system.
  370.  
  371.      In an ideal computer language we would look for a true reflection of
  372.      this natural information flow with as few as possible grammatical or
  373.      formal impositions which do not contribute to the main logical sequence.
  374.      If your code becomes incomprehensible and convoluted, particularly to
  375.      an unfamiliar reader, this can be a fair indication that your program
  376.      will prove both slow and inefficient when executed.
  377.  
  378.      We ought to at least attempt to avoid extraneous functions which need
  379.      to be inserted simply to comply with the needs of the programming
  380.      environment rather than the logic of the problem being solved.
  381.  
  382.      A very real advantage of FORTH's free use of language is that it is
  383.      much easier to learn and develop a software system which is expressed
  384.      entirely in your own words, using expressions which have memorability
  385.      and meaning for you, than it is to try to learn someone else's
  386.      formulations.  It is a wonderful asset to any programmer to be able
  387.      express ideas in your own way.
  388.  
  389.      It is even better when you can get instant feedback from the computer
  390.      so that you can see at once whether or not your ideas are working out.
  391.  
  392.      This is especially true of machines like the Amiga where sheer size
  393.      and complexity can make learning new software systems quite arduous,
  394.      and even more so if you have to wait for a long process of compilation,
  395.      linking, and loading before finding that you have made some obscure
  396.      syntactical error.
  397.  
  398.      FORTH is ideal because not only can you express yourself in your own
  399.      language very freely, but you get an instant response from the Amiga
  400.      on a word-by-word basis if required.
  401.  
  402.      *********************
  403.      Human versus computer
  404.      *********************
  405.  
  406.      We have just said that FORTH make uniquely excellent use of human
  407.      language and allows the programmer great freedom of expression and
  408.      interactivity.
  409.  
  410.      Let us now see things from the computer's point of view.
  411.  
  412.      Another side of the human/machine relation is the unwanted imposition
  413.      by the high level computer language of artificial logical processes
  414.      on the way the computer's CPU carries out its work.
  415.  
  416.      There is usually a natural and efficient way for the processor to carry
  417.      out any particular software task, and to the extent that a high level
  418.      language forces the CPU to deviate from optimum machine code processes
  419.      the language becomes slow and inefficient.
  420.  
  421.      Not many high level languages come anywhere near optimising their final
  422.      machine code constructs, mainly because they lean quite heavily towards
  423.      cushioning the end user from "bothering" with CPU operations.
  424.  
  425.      FORTH was written for people who understand how computers work and who
  426.      do concern themselves with the nature of CPU operations!
  427.  
  428.      As we have seen, FORTH was conceived initially as a super software tool
  429.      designed by a computer professional for his own use: it does assume a
  430.      little more knowledge of what is going on behind the scenes than other
  431.      languages.  FORTH requires more from the programmer but gives a lot in
  432.      return because you have more control over low level processes.
  433.  
  434.      FORTH has been likened to a high level assembler, and from the point
  435.      of view of the CPU FORTH code is very efficient.  It is actually quite
  436.      simple to write pure machine code directly from the high level HeliOS
  437.      interpreter/compiler, and converting individual time-critical routines 
  438.      into fast machine code is very easy.
  439.  
  440.      ********************
  441.      Having it both ways?
  442.      ********************
  443.  
  444.      All the above seems to be saying, apparently paradoxically, that FORTH
  445.      not only optimises the human side of the equation but also the machine
  446.      side.  Can this really be so?  Amazingly enough it can, but of course
  447.      there are, inevitably in this imperfect world, one or two compromises.
  448.  
  449.      You gain power over the CPU only under threat of severe punishment if
  450.      you make a mistake.  You need to be very careful what you are doing,
  451.      and you will end up understanding intimately how the computer works
  452.      whether you like it or not!  FORTH gives you extreme power but very
  453.      little cushion against any errors you may make: you are controlling
  454.      the computer at a very low level and with very high efficiency.
  455.  
  456.      At least, though, you do get to know your mistakes at once rather than
  457.      waiting half an hour or so to have them compiled and linked before all
  458.      is revealed!  You may not be protected from the inevitable consequences
  459.      of faulty code, but you can at least be reassured that the CPU is not
  460.      wasting valuable time checking for all your possible errors.  You will
  461.      also only write optimised code if you learn to think in a way consistent
  462.      with the logic of the computer, and FORTH is very much a transparent
  463.      link between man and machine - a double edged sword.
  464.  
  465.      *******************
  466.      FORTH functionality
  467.      *******************
  468.  
  469.      Now let us have a very brief look at how the language functions.
  470.  
  471.      The first thing you must realise is that a FORTH system can be built
  472.      and configured in many different ways and in general the main three
  473.      performance criteria of program size, speed, and code portability tend
  474.      to be somewhat mutually exclusive.
  475.  
  476.      All software involves a trade-off between various conflicting factors,
  477.      and different strategies are required when designing a FORTH system
  478.      for modern 16-bit computers as against the tiny machines on which many
  479.      early FORTH systems were developed.
  480.  
  481.      FORTH belongs to the general category of what are called "threaded
  482.      interpretive languages", otherwise abbreviated to "TIL", and these
  483.      essentially use a system of pointers and stacks to control the complex
  484.      threading together of code "units" (words) which themselves consist of
  485.      further threaded sub-sections.
  486.  
  487.      The lowest level FORTH code units are machine coded functions, and are
  488.      very fast: the highest level FORTH commands can be composed of many
  489.      threaded sub-levels, and can be very slow and inefficient.  The way
  490.      a FORTH system is designed and built has huge effects on performance,
  491.      because the core command set of any FORTH can consist of fast low
  492.      level functions or slow high-level "compound" functions.
  493.  
  494.      The machine code functions within a FORTH system are the "Workers",
  495.      and they are controlled by the higher level functions.  A cunningly
  496.      devised FORTH system can contain very few of these "workers", but can
  497.      put them to a very varied and ingenious use.  The problem is that if
  498.      you only have a few "workers" running around doing all the work, the
  499.      system can get rather slow.
  500.  
  501.      It is possible to have a special "Worker" for every function, so that
  502.      every job gets done very quickly and efficiently: this type of system
  503.      consumes more memory and is harder to port from machine to machine,
  504.      but it is lightning fast!  HeliOS is a FORTH based system with more
  505.      pure machine code "Workers" than any other FORTH on any computer.
  506.  
  507.      Higher level FORTH words usually consist of instructions to tell other
  508.      instructions to tell "worker" routines to do something.  This means
  509.      that in FORTH you get a hierarchical "nesting" of levels of command
  510.      and execution, with machine coded "worker" routines combined with
  511.      high level "instructional" routines.
  512.  
  513.      There is no limit on "nesting" or combination of levels, high or low
  514.      level constructs being easily and freely combined, and it is not even
  515.      necessarily obvious to what extent any FORTH function combines high
  516.      and low level code.  This leaves a great freedom for FORTH systems to
  517.      vary in character, and it is true to say that designing a good FORTH
  518.      is something of an art form: all FORTHs are different and individual.
  519.  
  520.      To a great extent your programming efforts will build upon the basis
  521.      provided by a set of central core routines in your FORTH system which,
  522.      as we have said, may be written in high or low level FORTH code.
  523.  
  524.      For speed it is vital that these central core routines be  written in
  525.      fast machine code rather than high level FORTH.
  526.  
  527.      This distinction must be fully appreciated, so always bear in mind
  528.      that any complex FORTH word can be written at various levels, from
  529.      pure machine code to densely nested high level structures built upon
  530.      very few machine code building blocks.
  531.  
  532.      The FORTH interpreter glides gracefully between the pure machine code
  533.      and a form of vectored execution of listed code subsections.
  534.  
  535.      These code subsections can themselves be more lists of further nested
  536.      subsections, and the inner interpreter will thread everything together
  537.      effortlessly without you having any indication whether it is whizzing
  538.      through pure fast machine code or churning its way up and down many
  539.      levels of convoluted vector list nestings.
  540.  
  541.      All this means that you need to know what you are doing with FORTH:
  542.      the language will allow you to write very good or very bad code!
  543.  
  544.      Interestingly, self-reference, recursion, and all the subtle concepts
  545.      currently popular in Artificial Intelligence can be implemented very
  546.      easily and well in FORTH, possibly rather more efficiently, in fact,
  547.      than in specialised "AI" languages such as "LISP".
  548.  
  549.      ************************
  550.      Implementations of FORTH
  551.      ************************
  552.  
  553.      One result of the way threaded interpreters work is that FORTH can run
  554.      with surprisingly few distinct machine code routines, which makes it
  555.      very easy to produce in readily machine-portable form.
  556.  
  557.      FORTH can be built like an inverted pyramid on a foundation of a very
  558.      small number of machine code routines, but in these circumstances the
  559.      language will run relatively slowly.  Faster FORTH systems will use
  560.      more pure machine coded core functions, as we said above.
  561.  
  562.      This means that there can be a huge difference in speed performance
  563.      between two FORTH systems which, in terms of the functions provided,
  564.      appear to be identical.
  565.  
  566.      Remember that FORTH high level code is very compact, so although it
  567.      runs more slowly than a heavily machine coded FORTH system, a high
  568.      level FORTH encoded system will be incredibly memory efficient.
  569.  
  570.      This feature can be put to good service if used judiciously and can
  571.      enable you to combine extremely fast code in time-dependent routines
  572.      with extremely compact code in memory critical areas.
  573.  
  574.      It is worth mentioning here that most implementations of FORTH are
  575.      based on an early version of the language intended for use on small
  576.      computers, with easy portability from one machine to another and very
  577.      thrifty memory usage the major design criteria.
  578.  
  579.      With these aims in view, this type of implementation carries a very
  580.      bare minimum of machine-specific machine language code, building the
  581.      majority of its functions by using FORTH code to build on other FORTH
  582.      code. This is a very memory efficient system and is amazingly compact
  583.      and easy to port between machines. It is actually a triumph of design
  584.      for the specific purpose for which it was written and can be used in
  585.      incredibly tiny memory spaces as well as being easily adapted to any
  586.      type of programming application.
  587.  
  588.      This memory economy and portability may seem quite a good idea, but on
  589.      an Amiga we are looking for speed mainly, we have plenty of available
  590.      memory space, and portability is not generally an issue.
  591.  
  592.      Since all FORTH functions can be coded in high level compiled FORTH
  593.      code or pure machine code, the extent to which the designer of a FORTH
  594.      system uses fast machine code in the main core routines determines the
  595.      speed and efficiency of programs written using that implementation.
  596.  
  597.      For our Amiga FORTH we certainly require a tightly written fast machine
  598.      code implementation which doesn't worry too much about using up a few
  599.      extra bytes in the quest for speed.
  600.  
  601.      It was for reasons of memory economy on tiny machines that the original
  602.      FORTH "virtual memory" disk-based source code editing and storage system
  603.      was used in many early FORTHs.  This system imposed very severe source
  604.      code format restrictions which many FORTH enthusiasts became accustomed
  605.      to, making them unwilling to move on to more freely expressive systems.
  606.  
  607.      The old ingenious but often crude system traditionally incorporated only
  608.      a primitive source code editing facility, and has been an anachronism
  609.      which has crippled many versions of FORTH, especially on 8-bit computers.
  610.  
  611.      On the Amiga this kind of "primitive" system is totally unacceptable
  612.      and must be avoided at all costs.  It is absolutely essential to have
  613.      a good, fast, full-featured source code editor which is totally memory
  614.      resident if you are going to manage any kind of really efficient code
  615.      development.
  616.  
  617.      The use of source code "screens" in FORTH, which you may see mentioned
  618.      in books, is another throwback to the old disk buffering systems, and
  619.      should be avoided at all costs since it places unnecessary constraints
  620.      on the formatting of your source code.
  621.  
  622.      It is best to employ a system which accepts source code text in free
  623.      format so that you can write your programs in a style which you find
  624.      comfortable and readable to suit your own taste.
  625.  
  626.      *******************************************
  627.      FORTH program structure and Amiga specifics
  628.      *******************************************
  629.  
  630.      In its purest form the code of your FORTH's core consists of a series
  631.      of tightly written machine code routines threaded together using a very
  632.      short connecting routine which hardly compromises performance at all.
  633.  
  634.      Obviously, however, every subdivision of the overall program carries
  635.      this small connective overhead, so the optimum strategy is to avoid
  636.      lots of short routines patched together every time we do a commonly
  637.      needed task.  Ideally we should have a predefined pure machine code
  638.      routine for every important and commonly used function to cut down
  639.      language overheads to a minimum.  Obviously this takes extra memory
  640.      space, but this is a commodity which we can, within reason, afford to
  641.      trade off in our quest for speed.
  642.  
  643.      In particular, internal housekeeping tasks such as stack handling need
  644.      to be kept as short as possible, and the "traditional" FORTH method of
  645.      using combinations of very few primitive functions is not ideal.
  646.  
  647.      Whilst traditional FORTH's very few stack control words may be patched
  648.      together to create very powerful compound words, this can cause a very
  649.      significant and unnecessary loss of speed.  We can afford the memory to
  650.      have a comprehensive fast-coded set of system routines pre-prepared,
  651.      and again these should be part of an efficiently machine-coded FORTH
  652.      core.
  653.  
  654.      Incidentally it is perhaps worth pointing out here that the excessive
  655.      combination of primitive FORTH words produces code which is unreadable,
  656.      inefficient and hard to debug.  It has often seemed to be a masochistic
  657.      obsession of FORTH programmers to write long convoluted sections of
  658.      code which do nothing other than rearrange the system stack using
  659.      totally confusing and unreadable combinations of a few stack control
  660.      primitives!
  661.  
  662.      It sometimes seemed as though stack management was the essence of FORTH
  663.      programming, whereas in truth stack manipulation is literally a waste
  664.      of CPU time and should be kept to a bare minimum.
  665.  
  666.      Don't be tempted into seemingly "clever" juggling with multiple items
  667.      on the stack; keep stack usage simple, fast, and elegant, and you will
  668.      find that a prime source of confusion and error will be eradicated.
  669.  
  670.      FORTH systems which only provide a very austere and crude set of core
  671.      functions require a lot of extension work to create all the tools
  672.      required for serious programming.  Even if you are prepared and able
  673.      to write these extensions yourself, or even if the extensions are made
  674.      available in the form of FORTH source libraries, the bad news is that
  675.      these will be compiled as high level constructs and will carry a very
  676.      significant speed penalty over low level core machine code routines.
  677.  
  678.      This penalty will overspill into all your subsequent code, so remember
  679.      that high performance really starts with the quality of your basic
  680.      system and the low level routines with which you construct your more
  681.      magnificent creations.
  682.  
  683.      Remember that high level FORTH code is by nature very compact indeed
  684.      and even an unexpanded Amiga A500 is a HUGE computer for a FORTH
  685.      environment.  Forget traditional FORTH purist ideas concerning the
  686.      "elegance" of managing complex tasks using a tiny array of words.
  687.  
  688.      Build yourself a really comprehensive set of fast software tools and
  689.      indulge yourself in the luxury of a language to a large extent
  690.      designed by you with your own specific needs in mind. Design your low
  691.      level system well and your high level constructions will be fast and
  692.      easy to write.
  693.  
  694.      ***************
  695.      Further reading
  696.      ***************
  697.  
  698.      We cannot here go into a full and detailed discussion of the actual
  699.      coding and function of all the many standard FORTH words.  Many good
  700.      FORTH textbooks are now available which cover this subject admirably,
  701.      and two books by Leo Brodie, "Starting FORTH" and "Thinking FORTH",
  702.      stand out as being particularly well-informed and instructive.
  703.  
  704.