2 पॉइंट द्वारा GN⁺ 2023-07-18 | 1 टिप्पणियां | WhatsApp पर शेयर करें
  • Forth की शुरुआत Chuck Moore द्वारा लंबे compile–punch card workflow को घटाने के लिए बनाए गए छोटे interpreter से हुई, और यह आगे चलकर editor, operating system और compiler तक को अपने में समेटने वाले system के रूप में विकसित हुआ
  • postfix notation (RPN), data stack और words (dictionary) कोई अजीब syntax नहीं थे, बल्कि कई constraints वाले hardware पर waste और दोहराए जाने वाले काम को घटाने के लिए की गई design choices थीं
  • Forth का core :, ;, IF, comments और variables जैसे elements तक को language के भीतर define और redefine कर सकने वाली bootstrap-capable structure में है
  • JonesForth, nasmjf, PlanckForth और sectorforth जैसे implementations दिखाते हैं कि Forth कुछ दर्जन से कुछ सौ bytes या मुट्ठीभर primitives से शुरू होकर interpreter, compiler, assembler और DSL तक expand हो सकता है
  • spacecraft, Open Firmware, Jupiter Ace, Canon Cat, Harris RTX2010 और GreenArrays के examples दिखाते हैं कि Forth का interactive development, छोटे code, hardware-friendly design और low-power computing से गहरा रिश्ता है

Usenet की legends से Forth की पड़ताल तक की शुरुआत

  • Dave Gauer ने Forth को खोजने और उसे computing history के संदर्भ में देखने की अपनी निजी यात्रा को संकलित किया; यह page एक छोटी presentation के slides से adapted है
  • 1990s में Usenet के comp.*, खासकर comp.lang.* groups, programming सीखने और उस पर चर्चा करने की महत्वपूर्ण जगह थे
    • Linus Torvalds की Linux की पहली announcement भी 1991 में comp.os.minix पर आई थी—इसे एक example के रूप में mention किया गया है
    • 1990s के अंत में Perl ने early Web पर CGI के जरिए dynamic pages और form handling की language के रूप में बड़ी भूमिका निभाई
  • Usenet के दौर में मिले Lisp-family languages, Y combinator, The Little Schemer और The Story of Mel जैसी legends ने Forth को लेकर curiosity पैदा की
  • Forth पहली बार “इतनी flexible language कि integer की value भी बदल सके” वाली कहानी के रूप में सामने आया, और Chuck Moore को कुछ screens के code से कोई भी program लिख सकने वाले “mad wizard” की तरह याद किया गया
  • James Hague की programming in the twenty-first century blog series Forth की पड़ताल शुरू करने का आखिरी trigger बनी

RPN और stack सिर्फ शुरुआती बिंदु हैं

  • Forth का first impression postfix notation या Reverse Polish Notation (RPN) है
    • आम infix notation 3 + 4 के विपरीत, Forth में operator को operands के बाद रखा जाता है, जैसे 3 4 +
    • Forth example 3 4 * 5 6 * + . 42 print करता है
    • dc में वही calculation 3 4 * 5 6 * + p के रूप में लिखी जाती है
  • RPN बिना parentheses के nested expressions को मनचाहे क्रम में calculate करने देता है
    • 3 4 * से 12 बनता है, 5 6 * से 30 बनता है, और आखिरी + से 42 बनता है
    • Forth में . stack से value निकालकर print करता है
  • HP-35 calculator RPN को computer science के बाहर भी पहचान दिलाने वाला एक महत्वपूर्ण historical example है
    • 1970s की शुरुआत में HP के पास powerful desktop calculators थे, और HP-35 को उस calculating power को “shirt pocket” calculator में डालने के example के रूप में पेश किया गया
    • HP calculators RPN syntax के लिए famous थे; RPN efficient था, लेकिन outsiders को कुछ हद तक कठिन लगता था
  • दूसरी विशेषता data stack है
    • PUSH, POP, SWAP, DUP जैसी operations Forth words भी हैं
    • number input करने पर वह stack पर चढ़ता है, और operator stack से values निकालकर calculation करता है और result को फिर से stack पर रखता है
  • stack intermediate values को नाम देने की जरूरत कम करता है
    • कई imperative languages में result2, matched_part3 जैसे temporary names चाहिए होते हैं
    • बिना नामों के काम करने के इस तरीके को implicit, tacit या point-free programming भी कहा जाता है
    • Forth को सिर्फ RPN और stack से समझना पर्याप्त नहीं है, लेकिन इसके historical origins को समझने के शुरुआती बिंदु के रूप में यह सही है

Concatenative programming और Joy, और Forth का फर्क

  • Forth की पड़ताल करते समय concatenative programming की concept सामने आती है
    • applicative example है eat(bake(prove(mix(ingredients))))
    • concatenative example है ingredients mix prove bake eat
    • functions क्रम से list होती हैं, और हर step की value implicitly अगले step को pass होती है
  • Joy concatenative language का representative example है
    • Manfred von Thun ने John Backus के 1977 ACM Turing Award lecture “Can Programming Be Liberated from the von Neumann Style?” से inspiration ली
    • Joy में हर function एक stack को input के रूप में लेता है और एक stack को output के रूप में return करता है
    • program functions की ऐसी list है जिसे left से right पढ़ा जाता है
  • Joy की quotation और combinator variables के बिना calculation का तरीका दिखाते हैं
    • JavaScript का [1, 2, 3, 4].map(n => n + 1) [2,3,4,5] बनाता है
    • Joy में [1 2 3 4] [1 +] map से वही result मिलता है
    • Joy का [] ऐसा quotation mechanism है जो data के साथ-साथ programs को भी रख सकता है
  • Combinator को function input के रूप में लेने वाले higher-order function की तरह handle किया जाता है
    • map() list traversal boilerplate को abstract करने का example है
    • I, K, S combinators और SKI calculus universal computation को explain करने वाले constructs के रूप में आते हैं
    • सिर्फ S और K भी universal computation के लिए एक complete system बन सकते हैं
  • Forth भी execution token और EXECUTE word से higher-order functions बना सकता है
    • FOO EXECUTE उस word को execute करता है जिसे FOO word ने return किया है
    • MAP, FOLD, REDUCE जैसे combinatorial words define किए जा सकते हैं
  • Forth और Joy की surface syntax मिलती-जुलती है, लेकिन expressions interpret करने का तरीका अलग है
    • Forth में 2 3 + को “2 और 3 को stack पर push करके जोड़ता है” की तरह पढ़ा जाता है
    • Joy में इसे “function 2, 3, + की composition function 5 के समान है” की तरह पढ़ा जाता है
    • Forth abstract concatenative computing principles से ज्यादा hardware के साथ interaction में विकसित हुई language के करीब है

Chuck Moore ने Forth बनाने तक का रास्ता

  • 1958 में Chuck Moore ने Smithsonian Astrophysical Observatory और MIT के दौर में Fortran और IBM 704 का इस्तेमाल किया
    • Moore ने Forth, the Early years में कहा, “Compiling took 30 minutes...you got one shot per day”
    • IBM 704 एक वैक्यूम-ट्यूब कंप्यूटर था जो पूरा कमरा भर देता था, और साथ में यह उद्धरण भी आता है कि वह औसतन करीब हर 8 घंटे में खराब हो जाता था
  • Moore ने recompilation कम करने के लिए प्रोग्राम को नियंत्रित करने वाले कार्ड पढ़ने वाला एक सरल interpreter बनाया
    • बिना recompile किए कई satellites के लिए अलग-अलग equations तैयार किए जा सकते थे
    • free-form input format, Fortran के formatted column की तुलना में इंसानों द्वारा लिखने के लिए अधिक भरोसेमंद था
    • यही सिस्टम बाद में Forth कहलाने वाले सिस्टम की उत्पत्ति बना
  • 1961 में Stanford में Moore ने Burroughs B5500 का उपयोग करते हुए CURVE नाम का mathematical application लिखा
    • CURVE में अधिक परिष्कृत interpreter, data stack, और control flow operator शामिल थे
    • इसकी capabilities मोटे तौर पर IF ELSE DUP DROP SWAP + - * जैसे Forth words के बराबर थीं
    • parameter stack और नई procedure define करने की क्षमता जोड़ी गई, जिससे Forth जैसी संरचना दिखाई देने लगी
  • 1965 के आसपास teletype, paper tape, timesharing, और interactive terminal environment आने के साथ Forth और अधिक interactive हो गया
    • KEY, EMIT, CR, SPACE, DIGIT जैसे words जोड़े गए
    • इन words ने Moore के सिस्टम को program editor में बदल दिया, और user program के अंदर ही program को edit कर सकता था
  • 1968 में Mohasco में Moore ने IBM 1130 पर काम करते हुए इसे “FORTH” नाम दिया
    • IBM 1130 16-bit, 8KB RAM configuration वाला था, और file names 5 characters तक सीमित थे
    • Moore का “fourth generation” system 5-character सीमा के कारण FORTH बन गया
    • इसी दौर में return call stack और word dictionary जोड़े गए
  • Forth की नामित word dictionary मुख्य abstraction की तरह काम करती है
    • : DOUBLE DUP + ;
    • : QUAD DOUBLE DOUBLE ;
    • return stack न हो तो DOUBLE के execute होने के बाद QUAD की बची हुई position पर वापस नहीं लौटा जा सकता

NRAO, पोर्टिंग, indirect threaded code

  • 1970 के दशक की शुरुआत में Moore ने National Radio Astronomy Observatory में radio telescope के लिए computer control software लिखा
    • NRAO की नीति minicomputer पर Fortran इस्तेमाल करने की थी, लेकिन Moore को पिछली सफलता के आधार पर Forth इस्तेमाल करने की अनुमति मिली
    • spectral-line observing में, collection के दौरान spectra दिखाए जा सकते थे और least-squares से line-shapes fit किए जा सकते थे
    • इस काम ने online data reduction की state-of-the-art को आगे बढ़ाया, और astronomers ने इसका उपयोग inter-stellar molecules खोजने और map करने में किया
  • Elizabeth “Bess” Rather ने Forth को अपनाया और 1972 में पहला Forth manual लिखा
    • बाद में उन्होंने Chuck Moore के साथ FORTH, Inc की co-founding की
    • 2006 में retirement तक वे Forth language की प्रमुख experts और promoters में से एक रहीं
  • Forth को IBM 360/50, Honeywell 316, Honeywell DDP-116, DEC PDP-11 जैसी कई machines पर port किया गया
    • H316 के लिए Forth implementation कंप्यूटर पर ही program किया गया था और अन्य Forth बनाने में इस्तेमाल हुआ, इसलिए इसे पहला complete, stand-alone implementation माना जाता है
    • PDP-11 Unix और C programming language के इतिहास से जुड़ा एक महत्वपूर्ण minicomputer है
  • कई machines पर port किए जा सकने के पीछे indirect threaded code था
    • यहां threaded code का concurrency या multi-threaded programming से संबंध नहीं है
    • threaded code, subroutine call instruction की जगह subroutine address की list store करता है
    • address list चलाने के लिए code interpreter की जरूरत होती है
  • indirect threaded code उन addresses की list store करता है जो code को सीधे point नहीं करते, बल्कि code को point करने वाले address को point करते हैं
    • दूसरे indirection की वजह से अलग-अलग तरह के subroutine के लिए अलग inner interpreter रखा जा सकता है
    • Forth में outer interpreter वह हिस्सा है जिससे user terminal पर interact करता है, और inner interpreter word के बाकी हिस्से को execute करता है
  • इस बिंदु तक Forth एक साधारण command interpreter से आगे बढ़कर interactive language, editor, operating system, code storage and execution method बन गया था
    • postfix notation, stack-oriented, concatenative style, interpreted, machine architecture के लिए highly adaptable, और extremely compact जैसे गुण साफ़ दिखते हैं
    • Forth कोई ऐसी language नहीं थी जिसे 1958 में एक बार में design कर दिया गया था; यह Moore द्वारा अपनी जरूरतों और नए hardware के मुताबिक बढ़ाई गई चीज़ का परिणाम था

Forth को समझना है तो Forth implement करना होगा

  • “To understand Forth, you have to implement a Forth” वाली सलाह में महत्वपूर्ण शब्द a Forth है
    • Chuck Moore के नजरिए से Forth system तब सबसे अच्छा होता है जब उसे system और task के हिसाब से custom-tailored किया जाए
  • author ने JonesForth source पर काम करने से पहले assembly और Linux ABI की basics सीखने के लिए Assembly Nights लिखा
    • JonesForth GNU GAS assembler में लिखे i386 assembly source का उपयोग करता है
    • यह केवल 32-bit है और Linux system call ABI को सीधे इस्तेमाल करता है
    • author ने JonesForth को NASM assembler पर चलने वाली copy में port करने में करीब 1 साल लगाया, और यही port nasmjf है
  • nasmjf porting process में traditional indirect threaded Forth के काम करने का तरीका सीखा
    • JonesForth का jonesforth.S assembly part रखने वाली single file है, और Richard W.M. Jones ASCII art और explanations से interpreter flow समझाते हैं
    • author ने अपनी समझ को व्यवस्थित करने के लिए nasmjf source में कई ASCII art diagrams बनाए
  • NEXT macro word execute होने के बाद अगले word पर जाने वाला core flow है
%macro NEXT 0
    lodsd     ; NEXT: Load from memory into eax, inc esi to point to next word.
    jmp [eax] ; Jump to whatever code we're now pointing at.
%endmacro
  • lodsd memory से double word को eax में ले जाता है और अगली position की ओर point करने के लिए update करता है
  • jmp [eax] execution को वर्तमान में point की गई जगह पर ले जाता है, और उस subroutine का inner interpreter बन जाता है
  • traditional Forth में मूल रूप से code word और colon word होते हैं
    • code word machine code में लिखा primitive होता है
    • colon word Forth language में लिखा normal word होता है और : compiler से assembled होता है
    • colon word EXIT पर खत्म होता है, और EXIT return stack को handle करने के बाद flow को NEXT में जारी रखता है
  • Forth बहुत छोटे-छोटे टुकड़ों के साथ मिलकर running system बनाने वाली संरचना है
    • low level पर flexibility और simplicity मिलती है, लेकिन high level पर समझने में आसानी की कुर्बानी देनी पड़ सकती है

Code word, colon word, compiler एक ही interpreter पर चलते हैं

  • SWAP, DUP, DROP दिखाते हैं कि Forth primitive को machine code में कितनी सीधे तौर पर implement किया जा सकता है
DEFCODE "SWAP",SWAP,0
    pop eax
    pop ebx
    push eax
    push ebx
NEXT
DEFCODE "DUP",DUP,0
    mov eax, [esp]
    push eax
NEXT
DEFCODE "DROP",DROP,0
    pop eax
NEXT
  • JonesForth i386 call/return stack को Forth parameter stack के रूप में इस्तेमाल करता है, इसलिए stack operation को native pop और push से implement करता है
  • nasmjf में 130 code word हैं, और ज़्यादातर efficiency के लिए हैं
    • bootstrappable Forth system के लिए ज़रूरी machine-code word की संख्या की एक theoretical minimum होती है
    • primitive की संख्या बहुत ज़्यादा घटाने पर बाकी Forth code inefficient और टेढ़ा-मेढ़ा हो सकता है
    • Frank Sergeant का A 3-INSTRUCTION FORTH FOR EMBEDDED SYSTEMS WORK Motorola MC68HC11 पर सिर्फ 66 bytes की ज़रूरत वाले 3-instruction Forth को कवर करता है
    • sectorforth 512-byte boot sector में फिट होने वाला 16-bit x86 Forth है
  • colon word मौजूदा word की composition से नया word बनाने का तरीका है
: SDD SWAP DROP DUP ;
  • SDD का असर SWAP DROP DUP को क्रम से call करने जैसा होता है
  • indirect threaded code के नज़रिये से, colon word तीन code word के inner interpreter address के रूप में compile हुआ होता है
  • : और ; syntax जैसे दिखते हैं, लेकिन असल में Forth word हैं
    • : नया word name लेता है और compile mode चालू करता है
    • ; नई dictionary entry पूरी करता है और compile mode बंद करता है
    • user : को ही अपनी definition से replace करके Forth को extend या बदल सकता है
    • Forth का इकलौता syntax input token के बीच का whitespace माना जाता है
  • Forth में compiling का मतलब memory में word address या literal और push code डालना है
    • normal mode में word तुरंत execute होता है
    • compile mode में word address memory में store होता है
    • वही interpreter code execution और code compiling दोनों करता है
  • IF...THEN जैसे control structure भी Forth में ही implement किए जा सकते हैं
: IF IMMEDIATE ' 0BRANCH , HERE @ 0 , ;

: THEN IMMEDIATE DUP HERE @ SWAP - SWAP ! ;
  • IF जैसा fundamental element भी language के अंदर define होता है—यही “The programming language that writes itself” शीर्षक का मुख्य आधार है
  • nested comment ( ... ) भी jonesforth.f में Forth से implement किया गया है
    • comment तक language के अंदर implement होता है, और user इसे redefine कर सकता है या अपना comment तरीका जोड़ सकता है
    • यह example इस बात पर ज़ोर देता है कि Forth में native syntax लगभग नहीं के बराबर है

Dictionary और redefinition extensibility बनाते हैं

  • Forth dictionary पारंपरिक रूप से linked list इस्तेमाल करती है, और word matching सबसे नई entry से शुरू होती है
    • उसी नाम की सबसे नई word definition पुरानी definition को छिपा देती है
    • कोई भी word redefine किया जा सकता है, और assembly में पहली बार define किए गए word भी redefine हो सकते हैं
    • पहले से compile हुए word नाम नहीं बल्कि address store करते हैं, इसलिए redefinition से पहले वाले word के address की ओर ही इशारा करते रहते हैं
  • नई definition में उसी नाम के पुराने word को call करके word को extend किया जा सकता है
    • recursive word बनाते समय : currently compile हो रहे word को dictionary में छिपा देता है या inactive state में mark करता है
    • current word के अंदर खुद को call करने के लिए RECURSE चाहिए
  • Leo Brodie की Thinking Forth से लिया और बदला गया apples variable example Forth की flexibility दिखाता है
    • VARIABLE APPLES एक APPLES word बनाता है, जो call होने पर memory address को stack पर रखता है
    • 20 APPLES ! उस address पर 20 store करता है
    • APPLES ? value लाकर print करता है
    • Thinking Forth (PDF) मुफ्त में उपलब्ध है
  • लाल apples और हरे apples को अलग-अलग track करना पड़े तब भी मौजूदा APPLES इस्तेमाल करने वाला code बदले बिना रह सकता है
VARIABLE COLOR

VARIABLE REDS
: RED REDS COLOR ! ;

VARIABLE GREENS
: GREEN GREENS COLOR ! ;

: APPLES COLOR @ ;
  • नया APPLES भी address को stack पर रखने वाला word है, इसलिए मौजूदा APPLES ! और APPLES ? इस्तेमाल करने का तरीका बना रहता है
  • address बस active apple color के अनुसार REDS या GREENS में बदलता है
  • सामान्य language के नज़रिये से variable function में बदल गया लगता है, लेकिन Forth में variable भी address रखने वाला word है और function भी word है, इसलिए language level पर concept में कोई बदलाव नहीं होता
  • number जैसा दिखने वाला token भी dictionary word के रूप में redefine किया जा सकता है
: 4 12 ;
  • ." The value of 4 is " 4 . CR चलाने पर The value of 4 is 12 print होता है

Meow5 और “सबसे कम resistance वाला रास्ता” के रूप में Forth

  • JonesForth को NASM पर port करने वाला nasmjf Moore की प्रक्रिया को recreate करने के बजाय final product को बारीकी से observe करने वाली “master copy” जैसी training थी
  • इसके बाद लेखक ने Meow5 नाम का एक बेहद extreme concatenative programming experiment किया
    • Meow5 ऐसा experiment है जिसमें सारा code हमेशा जोड़ा और inline किया जाता है
    • Forth की तरह parameter stack concept इस्तेमाल करने के कारण यह concatenative है
    • यह एक thought experiment था कि function call address store करने के बजाय पूरी function copy store की जाए तो क्या होगा
  • Meow5 का core सभी word को 100% inline करना है
    • छोटे function call overhead से बचने वाली आम optimization inline expansion से इसकी शुरुआत हुई
    • DROP जैसे one-instruction primitive को कई jumps के जरिए call करना waste जैसा लगा
  • Meow5 “एक मज़ेदार छोटी failure” पर खत्म हुआ
    • machine code inlining खुद अच्छी तरह काम करता था, और output ELF executable भी intended तरीके से चलता था
    • समस्या string data थी, और strings को हर बार copy करना पड़ता था या जटिल तरीके से track करना पड़ता था कि कौन सा word string इस्तेमाल करता है
    • selective inlining देने वाला Forth का INLINE word बेहतर तरीका माना गया
  • string syntax में भी Moore की सलाह फिर से सही साबित हुई
    • Forth में " नाम का word इस्तेमाल होता है, इसलिए " Hello World." की तरह space चाहिए
    • Meow5 ने "Hello World." वाली natural quoting style और \n, \" जैसे escape sequence अपनाए
    • इस choice के कारण tokenizer और interpreter में जगह-जगह " की exception handling करनी पड़ी, और purity व simplicity कम हो गई
  • Forth के रास्ते पर चलना शुरू करने पर बाकी चीज़ें जैसे “अपने-आप लिखी जाती” चली जाती हैं
    • Forth bootstrapping है, metaprogramming है, और OS व IDE/editor बन सकता है
    • बात इस दावे तक पहुँचती है कि अगर आप पूरी तरह नई CPU architecture के लिए सबसे simple interpreter बनाते हैं, तो आप Forth जैसी किसी चीज़ को लिखने लग सकते हैं

छोटे Forth implementations और वास्तविक उपयोग

  • Forth, Forth के अंदर और अधिक Forth define कर सकता है, और कई Forth में assembler भी शामिल होता है
    • JonesForth के आखिर में Forth के अंदर चलने वाले assembler का stub है
    • ;CODE header को update करता है ताकि codeword DOCOL की बजाय assembled code की ओर point करे
  • PlanckForth हाथ से लिखा गया 1KB से कम binary Forth है
    • पूरा ELF binary और काम करने वाला Forth implementation 1KB से कम में फिट हो जाता है
    • bootstrap.fs शुरुआत के कठिन-से-समझ आने वाले operators से शुरू होकर कुछ सौ lines बाद पढ़ने में आसान Forth बन जाता है
  • छोटे implementations के examples Forth की बेहद compact होने की क्षमता दिखाते हैं
    • SmithForth David Smith का हाथ से लिखा machine code Forth है, जिसे 1,000 bytes और 1,000 lines वाले Forth system के रूप में पेश किया गया है
    • sectorforth 512-byte boot sector Forth है
  • Forth का उपयोग power plants, robotics, missile tracking systems, industrial automation, video games की embedded language, databases, accounting, word processors, graphics, calculation systems, Open Firmware boot loader, और अलग-अलग processors व microcontrollers में हुआ है
  • Open Firmware Sun Microsystems engineering से निकला एक अहम example है
    • interactive programming language पर आधारित होने के कारण नए hardware को कुशलता से test और bring-up किया जा सकता है
    • Space Shuttle ESN example में Open Firmware को hardware, software, plugin drivers और firmware itself तक debug कर सकने वाले tool के रूप में देखा गया है
  • Jupiter Ace 1982 का ब्रिटिश home computer था, जिसका operating system Forth था
    • OS और routine library 8KB ROM में फिट हो जाते थे
    • 1982 का एक quote है कि built-in Forth, interpreted BASIC से “10 गुना तेज” था और memory requirement आधे से भी कम थी
  • Canon Cat में 1987 में operating system Forth था, और 256KB ROM में OS, office suite और programming environment शामिल थे
    • यह Jef Raskin के keyboard-centric interface और design philosophy को hardware और software में लागू करने वाला product था
    • Forth interface में जाने की प्रक्रिया सबसे प्राथमिक user flow जैसी नहीं दिखती, लेकिन procedure पता हो तो कुछ key presses में संभव है

Space software और Forth

  • Forth के दिलचस्प उपयोगों में से एक space exploration है
    • spacecraft महंगे होते हैं और repair access मुश्किल या असंभव होता है, इसलिए software reliability महत्वपूर्ण होती है
    • NASA के Forth इस्तेमाल वाले projects की list लंबी है; original link गायब हो चुका है, लेकिन Wayback Machine और forth.com copies मौजूद हैं
  • Space Shuttle Small Payload Accommodations Interface Module(SPAIM) Forth इस्तेमाल करने का एक example है
    • development team ने PC पर Forth routines लिखे, उन्हें download किया और फिर interactively test किया
    • एक quote है कि STS-45 Shuttle mission के दौरान SPAIM flight software बिना problem के चला
    • SPAIM का Forth system multitasking कर सकता था, हर task का अपना stack था, और watchdog task stack state को monitor करता था
  • Space Shuttle Robot Arm Simulator एक complex device को एक programmer द्वारा 5 हफ्तों में develop करने का example है
    • उसे 3-axis joystick commands लेकर 50-foot लंबी, 6 joints और 6 coordinate systems वाली arm को control करना था
    • quote के अनुसार system में 14 अलग-अलग processes थे
    • एक quote है कि simulation testing इतनी thorough थी कि field installation के बाद executive control algorithm को बदला नहीं गया
  • Shuttle Mission Design and Operations System(SMDOS), JPL का ground-based control software था
    • SIR-B mission के दौरान कई hardware failures का field में software fixes से जवाब दिया गया
    • antenna problem और orbit changes के कारण नई data collection strategy की जरूरत थी, और SMDOS का उपयोग नई conditions के हिसाब से plan दिखाने के लिए किया गया
    • बहुत data खो गया, लेकिन rotate-capture-rotate-transmit तरीके से 20% data recover किया गया
  • Harris RTX2010 कई space applications में इस्तेमाल हुआ Forth hardware था
    • Forth को सीधे execute करना
    • 256-word depth वाले 2 hardware stacks
    • 8MHz clock और low latency
    • radiation-hardened characteristics
  • Rosetta और Philae को comet की orbit में spacecraft भेजने और lander को comet की surface पर उतारने वाले पहले mission के रूप में पेश किया गया है
    • Rosetta के Ion and Electron Sensor ने Harris RTX2010 का इस्तेमाल किया
    • Philae lander ने CDMS में 2 RTX2010 और ADS control के लिए 2 RTX2010 इस्तेमाल किए
    • Rosetta ने 2004 से 2014 तक यात्रा कर 67P/Churyumov-Gerasimenko से rendezvous किया
    • 67P 4km चौड़ा है और हर साढ़े 6 साल में Sun की परिक्रमा करता है
  • Philae ने harpoons और landing thruster failure के कारण surface पर low-gravity bounce झेला, लेकिन इतना robust था कि science mission का “80%” पूरा कर सका
  • 1993 का Galileo magnetometer code patch example भी आता है
    • magnetometer में RCA1802, 2KB RAM, 2KB ROM था, और इसे Apple II-based development system से Forth में program किया गया था
    • instrument code के बीच में एक खराब memory byte आ गया था, इसलिए patch करना पड़ा ताकि उस byte का उपयोग न हो
    • quote देने वाले व्यक्ति ने Lisp में उस instrument के लिए Forth development environment और hardware simulator scratch से लिखा, patch generate किया, और part-time काम करते हुए 3 महीने से कम लगे

Forth किसी single implementation से ज्यादा ideas और lineage जैसा है

  • Forth किसी एक representative implementation से ज्यादा shared concepts और lineage वाला idea है
  • Forth सीखना कठिन होने का एक कारण यह है कि “Forth” नाम का exclusive representative कोई single implementation नहीं है
    • शुरुआती Forth में से कुछ “Forth” नाम से भी पहले के थे
    • Forth standard, ANS Forth document से आगे चलता है, और Forth 2012 Standard व Forth200x committee का जिक्र आता है
  • Forth systems concepts और common words share करते हैं, लेकिन purpose-specific Forth की अपनी special vocabulary होती है
  • Chuck Moore ने 1973 में Forth, Inc की स्थापना की, बाद में Forth को कई systems पर port किया, और वे लगातार नए systems बनाते रहे
  • colorForth, Chuck Moore का बाद का environment है
    • source code के colors, standard Forth के कुछ punctuation की जगह लेते हैं और word को process करने का तरीका तय करते हैं
    • बताया गया है कि colors Forth की semantics को simplify करते हैं, compile speed बढ़ाते हैं, और Moore की कमजोर eyesight में भी मदद करते हैं
    • colorForth में अपना 63KB operating system शामिल है
  • Moore का मानना था कि features लगातार जुड़ते जाते हैं, software complex हो जाता है, और unintended consequences के डर से कोई उसे बदल नहीं पाता
    • इससे उनका argument यह बनता है कि जरूरत एक ऐसे dedicated programmer की है जो किसी single application को अपना career समर्पित करे, उसे बार-बार rewrite करे और perfect बनाए

मशीन-फ्रेंडलीनेस, Forth चिप, GreenArrays

  • Chuck Moore 1950 के दशक से software complexity के खिलाफ रहे हैं, और Windows, UNIX, DOS जैसे operating systems को न समझ में आने वाला या गैर-जरूरी मानने वाले quote सामने आते हैं
  • लेखक Moore की philosophy को Mechanical Sympathy के रूप में समझाते हैं
    • यह expression Formula One driver Jackie Stewart के quote से आया है, और software context में Martin Thompson के Why Mechanical Sympathy? में इस्तेमाल हुआ
    • Forth, electrical engineers और embedded system designers जैसे “hardware people” के बीच लोकप्रिय रहने की प्रवृत्ति रखता है
  • Chuck Moore का असली लगाव processor design में दिखता है
    • Harris RTX2000 और RTX2010 को मूल रूप से Moore के chip के तौर पर पेश किया गया है
    • Moore ने 1983 में Novix N400 gate array से hardware design करना शुरू किया, और सुधरा हुआ design Harris को बेचा गया, जिससे RTX chip बने
  • Moore ने 500 lines के Forth में लिखे अपने VLSI software OKAD से processor design किए
    • VLSI Design Tools (OKAD) बताता है कि 500 lines के colorForth में chip design के लिए जरूरी सब कुछ दिया गया है
    • OKAD, logic gate technology, circuit layout, electrical behavior simulation, और GDSII export शामिल करने वाला toolset है
  • Novix NC4000 एक ultra-fast processing engine है, जिसे high-level Forth commands को सीधे execute करने के लिए design किया गया था
    • सामान्य assembly language और internal microcode हटाकर performance हासिल की गई
    • dual stack architecture, subroutine implementation overhead को काफी कम कर देता है
  • GreenArrays chip को Moore की 2013 की presentation “Programming a 144-computer chip to minimize power” के केंद्र में पेश किया गया है
    • एक chip में 144 asynchronous computers होते हैं
    • idle core 100 nW, active core 4 mW इस्तेमाल करता है और 666 Mips पर run करने के बाद वापस idle state में लौट जाता है
    • जब सभी computers maximum speed पर चलते हैं, तो power consumption 550 mW, यानी करीब 0.5 W होता है
  • GreenArrays Architecture document “NO CLOCKS” पर जोर देता है
    • समझाया गया है कि सामान्य computers में clock generation और distribution standby में भी बहुत energy consume करते हैं, लेकिन कोई काम नहीं करते
    • GreenArrays को Multi-Core CPU नहीं, बल्कि Multi-Computer System के रूप में पेश किया गया है

सरलता, efficiency, और low-power computing का भविष्य

  • लेखक मानते हैं कि software और hardware लगातार complexity की ज्यादा layers जोड़ने की दिशा में बढ़ते रहे हैं
    • बड़े data centers की computing को सस्ता या free माना जाता है, क्योंकि उसका भुगतान अप्रत्यक्ष रूप से ad spend से होता है
    • बताया गया है कि असली भुगतान users का attention और personal data है
  • वे तर्क देते हैं कि आगे भी और ज्यादा inefficient और कमजोर तरीके से बनाए गए software तथा और तेज hardware requirements का इस्तेमाल करते रहने की कोई वजह नहीं है
  • Forth जैसी languages का इन क्षेत्रों में मजबूत भविष्य हो सकता है
    • छोटे और हर जगह मौजूद computers
    • solar power
    • बहुत कड़ी constraints वाली VM
  • Dennard scaling लगभग 2006 के आसपास लगभग रुक गई
    • छोटे transistors से higher speed और lower voltage संभव होने वाला संबंध current leakage और heat की वजह से लगभग 4GHz पर ठहराव तक पहुंच गया
    • Herb Sutter का The Free Lunch Is Over वह लेख है जो बताता है कि CPU performance improvement का तरीका hyper-threading और multicore की ओर शिफ्ट हो रहा है
  • Sutter मानते हैं कि applications को concurrency के हिसाब से redesign करना होगा, या ज्यादा efficient और कम wasteful code लिखना होगा
    • वे कहते हैं कि performance optimization कम महत्वपूर्ण नहीं, बल्कि ज्यादा महत्वपूर्ण होगी
  • आज IBM 704 से कहीं ज्यादा powerful microcontroller कुछ सिक्कों की कीमत में खरीदा जा सकता है, और उस पर powerful Forth system लिखा जा सकता है
    • microcontroller coin cell battery या छोटे solar panel से चल सकता है
    • इसके बाद यह बात आती है कि small-scale computing के लिए इससे ज्यादा अद्भुत समय कभी नहीं था
  • Forth को समझने के लिए “simple” और “easy” के फर्क पर विचार करना होगा
    • Rich Hickey का Simple Made Easy वह talk बताया गया है जिसने यह distinction कहने की सुविधा दी
    • Forth आसान नहीं है और हमेशा मजेदार भी न हो, लेकिन यह निश्चित रूप से सरल language है
  • Guy Steele का Growing a Language वह talk है जो दिखाता है कि language primitives से कैसे बनती है
    • जब user द्वारा define किए गए नए words primitive जैसे दिखते हैं, और primitive भी user-defined words जैसे दिखते हैं, तो seamless बड़ी language बनती है—Lisp के इस quote को Forth पर भी लागू किया जा सकता है
  • आखिरी आग्रह है कि अपने लिए perfect programming language बनाएं

1 टिप्पणियां

 
GN⁺ 2023-07-18
Hacker News की टिप्पणियाँ
  • Forth के बारे में पता चलते ही लगा कि इसे खुद बनाकर देखना चाहिए, इसलिए 2 साल पहले मैंने MiniForth सिर्फ एक हफ़्ते में बना लिया
    यह Blue Pill और मूल Arduino के लिए था, लेकिन host पर test कर सकूँ इसलिए इसे cross compiler के रूप में develop किया
    मैं इसमें इतना डूब गया था कि ऑफिस के बाद रोज़ इसे छेड़ने का समय आने का इंतज़ार रहता था, और कुल मिलाकर 5 weekday शामों और एक weekend में यह पूरा हो गया; Forth बनाना उतना ही मज़ेदार था
    मैं इस प्रक्रिया की ज़ोरदार सिफारिश करता हूँ, और ऐसा ही गहरा अहसास मुझे पहली बार Lisp macros से सामना होने पर हुआ था (https://www.thanassis.space/score4.html#lisp)
    • सही कहा। पहले साल की computer architecture class में LC-3 के लिए free-form final project करना था, और मैंने पहले RedPower 2 Minecraft mod में Forth को थोड़ा छुआ था, इसलिए लगा इसे implement करना मज़ेदार रहेगा
      उस semester में मेरा लगभग सारा free time उसी ने खा लिया, और एक दिन तो घड़ी देखकर पता चला कि मैं पूरी रात जागता रहा और सूरज निकलने लगा था
      मैंने असल जीवन में Forth का व्यावहारिक इस्तेमाल कभी नहीं किया, लेकिन शुरू से एक बनाना ऐसा अनुभव था जिसे लगभग धार्मिक अनुभव कहना गलत नहीं होगा
      यह Fred Brooks के उस विचार जैसा लगा कि programming किसी शुद्ध आसवन की तरह है: programmer कवि की तरह लगभग शुद्ध विचार-सामग्री पर काम करता है, सिर्फ कल्पना से हवा में महल बनाता है, और program एक तरह का वास्तविक जादू है जिसमें सही मंत्र लिखते ही स्क्रीन पर वह चीज़ जीवित हो उठती है जो पहले वहाँ थी ही नहीं
    • पूरी तरह सहमत। बहुत पहले मैंने ZX81 BASIC में एक Forth compiler बनाया था
      स्क्रीन पर 1000 संख्याएँ print करने में 19 सेकंड लगते थे, और फिर वह काम लगभग तुरंत होने लगा
      उससे एक बड़ा सबक भी मिला: generated machine code देखकर लगा कि यह बिल्कुल perfect है और इससे बेहतर नहीं हो सकता, लेकिन अगले दिन वापस आकर मैंने इसे बहुत ज़्यादा तेज़ बना दिया
  • आप सोच सकते हैं, “यह अपने-आप को कैसे लिखता है? क्या बस functions लिख सकने की बात है?”
    कुछ हद तक हाँ, लेकिन Forth इससे आगे जाता है क्योंकि आप नए words define कर सकते हैं और उन्हें भाषा के अपने हिस्से की तरह बना सकते हैं
    इन words का इस्तेमाल built-in operations की तरह किया जा सकता है, और ये भाषा के काम करने के तरीके को भी बदल सकते हैं
    क्योंकि यह एक extensible language है, आप नए control structures define करने वाले या syntax बदलने वाले words भी बना सकते हैं, और ऐसी flexibility दूसरी भाषाओं में आम नहीं है
    इसी अर्थ में Forth को “अपने-आप को लिखने वाली” भाषा माना जा सकता है, क्योंकि programmer भाषा को बहुत बुनियादी स्तर पर customize और extend कर सकता है
    यह Lisp के macro system जैसा भी है: जैसे Lisp macros compile time पर transformation करके नए syntactic structures और domain-specific languages बनाने देते हैं, वैसे ही Forth के words syntax और functionality को बढ़ाने देते हैं
    • सुनने में अच्छा है, लेकिन लगभग 40 साल professional programmer के रूप में काम करते हुए मेरे लिए समस्या कभी syntax नहीं, हमेशा semantics रही है
      matrix multiplication जैसी किसी चीज़ में A = B + C * D; लिखना, हर function call को अलग-अलग लिखने से बेहतर हो सकता है, लेकिन असल में मुझे ऐसा मामला कभी नहीं मिला
      समझ नहीं आता कि मेरी imagination कम है, या यह सिर्फ किसी खास domain में उपयोगी है और मैं उस domain में कभी रहा नहीं, या Forth का syntax इतना गरीब है कि उसे usable बनाने के लिए extend करना पड़ता है, या लोग बस कार tuning की तरह भाषा को customize करना पसंद करते हैं
      लोग वास्तव में इसमें इतनी दिलचस्पी क्यों लेते हैं, यह मुझे समझ नहीं आता
  • इस समय सबसे दिलचस्प Forth projects में से एक Dusk OS है, जो Forth में लिखा गया 32-bit operating system है और इसमें अपना C compiler भी शामिल है; इसके कई ports पर भी काम चल रहा है
    https://duskos.org/
    • DuskOS के बारे में खुद ज़्यादा नहीं जानता, लेकिन ऐतिहासिक रूप से यह सही है कि Forth अक्सर “अपना खुद का operating system” हुआ करता था, हालाँकि आधुनिक पाठक operating system से जो उम्मीद करते हैं उसकी तुलना में वह काफ़ी कच्चा था
      8KB RAM में चलने वाली चीज़ से क्या उम्मीद करनी चाहिए, यह बात सही है, लेकिन भले ही वह primitive features देता था, ऐतिहासिक रूप से उसमें drivers या ज़्यादातर abstraction concepts लगभग नहीं थे
      code source से load होता था इसलिए धीमा था, और linking ज़्यादा से ज़्यादा blocks को सही क्रम में load करने जैसा था; नया program load करने के लिए पहले मौजूदा program हटाना पड़ता था
      program बंद होने पर मशीन को cold start कर देने वाले CP/M से यह बहुत बेहतर था, ऐसा भी कहना मुश्किल है, लेकिन कम-से-कम CP/M में BIOS और BDOS अलग थे
    • यह मेरी नज़र से छूट गया था। मैं अभी “Beyond the Collapse” पढ़ रहा था, और लगा कि Lisp या APL/J, C की अपनी समझ के बावजूद OS स्तर पर Forth की सरलता तक पहुँचना मुश्किल होगा
      अगले weekend से पहले इसे ज़रूर देखूँगा
  • मेरी पहली programming languages x86 assembly और Forth थीं। मेरे पिता Forth के दीवाने थे, और मैंने programming उन्हीं से सीखी
    किशोरावस्था में मैंने DOS के लिए कई x86 Forth systems बनाए, और आखिर में “Third” तक पहुँचा, जो काफ़ी polished ANS-compatible implementation था: https://github.com/benhoyt/third
    यह काफ़ी हैरत की बात है कि आप assembler सहित पूरी तरह bootstrapped Forth compiler कुछ हज़ार lines of code में पा सकते हैं
    कुछ समय पहले मैंने Forth Dimensions magazine के लिए मिलकर लिखा गया एक पुराना लेख भी फिर से टाइप किया
    Forth के ideas आज भी अच्छे हैं, लेकिन stack manipulation जल्दी उबाऊ हो जाती है और पढ़ना भी बहुत मुश्किल हो जाता है
    इसका अंदाज़ा https://benhoyt.com/writings/forth-lookup-tables/ के code examples, खासकर Search-Table, से लगाया जा सकता है। नाम रखना मुश्किल ज़रूर है, लेकिन बिना नाम के काम चलाना शायद उससे भी मुश्किल है
    • stack manipulation के उबाऊ हो जाने की वजह शायद यह है कि आप और मैं Chuck Moore नहीं हैं
      उसके code में आम तौर पर stack को इधर-उधर घुमाने की ज़रूरत बहुत कम पड़ती है। उदाहरण: http://www.merlintec.com/download/color.html
    • मैंने Third का source code थोड़ी देर देखा, दिलचस्प लगा
      bootstrap करते समय यह kernel.com को call करता दिखा; जिज्ञासा है कि kernel.com को आपने कैसे build किया
  • संबंधित Chuck Moore व्याख्यान: “Programming a 144 Computer Chip to Minimize Power” (2013)

https://www.youtube.com/watch?v=0PclgBd6_Zs
ऐसा कहा जाता है कि Forth ultra-low-power कंप्यूटरों को इस तरह चलाता है: GreenArrays 144-core asynchronous chip शिप कर रही है, और इसे सिर्फ 7 pJ/inst ऊर्जा चाहिए
idle core बिजली नहीं लेते (100 nW), और active core 4 mW पर तेज़ी से चलते हैं (666 Mips), फिर communication का इंतज़ार करते हुए idle स्थिति में लौट जाते हैं

  • “मैं हमेशा सोचता था कि आकार और गति सिर्फ amateurs के लिए महत्वपूर्ण हैं। लेकिन आधुनिक संदर्भ में वास्तव में सिर्फ तीन पैरामीटर मायने रखते हैं: power, power, power
    --Chuck Moore
    https://youtu.be/0PclgBd6_Zs?t=389
  • मैं सच में सोचता हूँ कि क्या ऐसी topology अगले कुछ वर्षों में फिर लौट सकती है
  • एक शानदार शुरुआती किताब Starting Forth है
    इसमें textbook में देखे गए सबसे आकर्षक illustrations में से कुछ हैं
    [0] https://www.forth.com/starting-forth/
  • बहुत पहले जब OS/2 आया था, एक दोस्त ने बताया था कि “OS/2 programs assembly में नहीं लिखे जा सकते, उन्हें C में लिखना होगा”
    उसी बात के विरोध में मैंने Forth/2 बनाया
    यह assembler में लिखा गया OS/2 के लिए direct-threaded native code Forth compiler था, Brian Matthewson ने इसके लिए एक शानदार manual लिखा, और इसके दर्जनों users भी मिले
    [1] https://sourceforge.net/projects/forth2/
  • थोड़ी अलग बात है, लेकिन मुझे याद है कि मैंने इस लेख के मूल presentation prototype रूप को पढ़ा था
    मूल रूप से यह अभी के लेख जैसा ही था, लेकिन इसमें text बहुत कम था और gaps ज़्यादा थे
    इसका ज़िक्र इसलिए कर रहा हूँ क्योंकि वह सामग्री अपने आधार website technology के लिहाज़ से भी minimalism की खूबसूरती दिखाती है
    [0] http://ratfactor.com/forth/forth_talk_2023.html
    [1] http://ratfactor.com/minslides/
    • क्या अपनी खुद की Forth बनाना और अपना सरल presentation software बनाना, इन दोनों के बीच कोई संबंध है? https://www.mankier.com/1/techtalk-pse
  • यह दिलचस्प है कि Rust में भी एक Forth compiler मौजूद है। संबंधित सामग्री [1] में है
    इसका उपयोग अभी थोड़ा अस्पष्ट लगता है[2], लेकिन इसे पहले भी यहाँ कवर किया गया है[3], और implementation मज़बूत दिखता है
    यह REPL Forth virtual machine नहीं है, लेकिन एक प्रभावशाली virtual machine implementation है
    अगर FORTH एक virtual machine है, तो मुझे हमेशा लगा है कि streaming data को संभालने के लिए जब छोटा और efficient system चाहिए हो, तब यह bare metal के क़रीब performance देने वाला बेहतरीन विकल्प हो सकता है
    इसमें operating system से जूझना भी नहीं पड़ता
    मुझे लगता है कि यह मूल रूप से वही काम standard CPU पर करना है जो FPGA करता है
    हालाँकि मैं इसे ऐसे special system के रूप में सोच रहा हूँ जिसमें ज़रूरी hardware से जुड़ने के लिए सारे drivers खुद लिखने की ज़रूरत न पड़े
    [1] https://docs.rs/rust-forth-compiler/latest/src/rust_forth_co...
    [2] https://docs.rs/fortraith/latest/fortraith/
    [3] https://news.ycombinator.com/item?id=23501474
  • मैंने एक और Forth खुद implement किया है
    इसका उद्देश्य मूल विचार की पूरी तरह faithful copy बनाना नहीं था, इसलिए यह थोड़ा अलग तरह से काम करता है
    लेख में कहा गया था कि आप Forth को खोज सकते हैं, इसलिए मैं अपनी implementation भी छोड़ रहा हूँ: https://github.com/loscoala/goforth
    सबसे बड़ा अंतर यह है कि इस Forth variant में source text पूरी तरह bytecode में बदल जाता है, और classical Forth वाले अर्थ में कोई runtime नहीं होता
    इसलिए bytecode को C में translate करना आसान है
    मैं इस Forth का उपयोग C code generate करने के लिए करता हूँ, और फिर उस code को दूसरे software में embed करके इस्तेमाल करता हूँ
    • ऐसा तरीका बहुत productive और clever हो सकता है, लेकिन यह बात लगातार ध्यान में रखनी चाहिए कि इस तरह के multi-language समाधान लंबे समय में maintenance headache बन जाते हैं
      एक सचमुच अच्छा open source project है जो ear training और sight reading का अभ्यास करने देता है, और वह लेखक की बनाई भाषा में लिखा गया है, जो JavaScript में compile होती है
      toolchain सेट करने की झंझट इतनी बड़ी है कि कई bugs के बारे में जानते हुए भी मैं उन्हें ठीक करने की हिम्मत नहीं कर पाया
      https://sightreading.training/
      https://github.com/leafo/sightreading.training
      यह project ‘Moonscript’ नाम की भाषा में लिखा गया है: https://github.com/leafo/moonscript
      Moonscript, Lua में compile होती है, और Lua, JS में compile होती है
      यह पागलपन है। शानदार पागलपन, लेकिन फिर भी इसने मुझे contributor बनने से रोके रखा