Guest User

Untitled

a guest
Sep 17th, 2024
44
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 12.37 KB | None | 0 0
  1. (*
  2. **** Hacheme ****
  3. *)
  4.  
  5. (*p
  6. \documentclass[a4paper]{report}
  7. \usepackage{amsmath,amssymb,amsthm}
  8. \usepackage{ocamlweb}
  9. \usepackage{makedix,glossaries,epigraph}
  10. \usepackage{filecontents}
  11. \usepackage{simpletrs}
  12. \usepackage{expl3}
  13. \usepackage[backend=biber,style=authoryear]{biblatex}
  14.  
  15. \addbibresource{references.bib}
  16. \includeonly{hacheme-command-definitions}
  17. \includeonly{hacheme-glossary-definitions}
  18. \includeonly{hacheme-index-definitions}
  19. \makeindex
  20. \makeglossaries
  21.  
  22. \begin{document}
  23.  
  24. The booklet before you presents the source code for \hacheme, an implementation of the \hachemestd, in a literate manner. This is not a textbook, nor is it a tutorial or a work of academia. This is merely a \litprogram. This also allows us to use \formeth{}s to, albeit partially, \verif\ this program --- and furthermore, delve into the history of \scheme\ and \lisp.
  25.  
  26.  
  27. \section*{Map to the program}\label{sec:prog_map}
  28.  
  29.  
  30. \section{A brief history of \lisp}\label{sec:lisp_hist}
  31.  
  32. \epigraph{"Ut therefore seems to be desirable to attempt to construct an artificial language which a computer can be programmed to use on problems and self~-reference. It should correspond to English in the sense that short English statements about the given subject matter should have short correspondents in the language and so should short arguments or consjectural arguments."}{J. McCarthy}
  33.  
  34. In the mid-1950s, \jmccarthy, an assisstant professor at \dartmouth, was presented the language \ipl; to which he showed no excitement due to its low level and rough shape\parencite{stoyan1984early}. In a surviving archival note, he prescribes \textit{the problem of programming a computer} into two bullet-points; the first being that "there does not exist an adequate language for human beings to describes procedures to each other", in a language which he felt like must have been \textit{explicit, univeral and concise} -- and the second issue he found was that computers at the time needed extra "engineering consideration" to be able t ounderstand such language\parencite{stoyan1984early}.
  35.  
  36. In his visit to \ibm, he was impressed by the success at engineering of the \fortran\ language by a team led by \jbackus. During September and October of that year, \jmccarthy\ worte some memos about the new language he was brewing in his mind\parencite{stoyan1979lisp}. McCarthy claimed he "did not understand Church's paper" so \lambdacalc\ did not play much of a role in early construction of \lisp\footnote{However, as we see later, \lambdacal\ plays a huge role in evolution of \scheme}.
  37.  
  38. % TODO: Complete history of lisp
  39.  
  40. \section{History of \scheme}
  41.  
  42.  
  43. % TODO: Complete history of scheme
  44.  
  45.  
  46. *)
  47.  
  48. (*
  49.  
  50. \section{\moduledef{Syntax}}\label{sec:mod_syntax}
  51.  
  52. \subsection{About the \absyn\ of \scheme}
  53.  
  54. The \modsty{Syntax} module defines the \syntax\ for \hacheme. As mentioned in \ref{sec:about_scheme}, the language uses fully-parenthesized \index{prefix} notation, or in other words, \sexpr. The \modsty{Syntax} module reflects this, but also, encodes several \textit{peculiarities} of the \scheme-flavored \sexp.
  55.  
  56. Where we to just use a \textit{basic} syntax for an \sexp\ language, we would have sufficed to \sexpatom{}s and \sexplist{}s. But our aim is to encode the syntax of \scheme, not just a run-off-the-mill \sexp\ syntax!
  57.  
  58. That is why \modsty{Syntax} module represents the \absyn\ for \scheme, and adds extra \hacheme-specific elements to it. In reality, the best way to convey the \absyn\ is using \sexp (\parencite{turbak2008design}\refextsection{2.3}). So languages like \scheme\ which use fully-parenthesized syntax are not only easier to parse, but each consturct conveys more info \textit{symbolically}.
  59.  
  60. The \scheme\ syntax is made up of \datum{}s which, in turn, form an \expr.
  61.  
  62. % TODO: Complete this section
  63.  
  64. *)
  65. (*
  66. \moduledefbegin{Syntax}
  67. *)
  68. module Syntax = struct
  69. (*
  70. \marktype{Syntax}{ProductType}{t}{The base type for \modsty{Syntax}}
  71. *)
  72. type t =
  73. { contents : expr list
  74. ; location : Location.t
  75. }
  76.  
  77. (*
  78. \marktype{Syntax}{SumType}{datum}{The \datum{}s, please see \ref{subsec:syntax_datum}}
  79. *)
  80. and datum =
  81. | DATUM_Boolean of bool
  82. | DATUM_String of string
  83. | DATUM_Char of char
  84. | DATUM_Label of label * datum
  85. | DATUM_List of datum list
  86. | DATUM_Symbol of label
  87. | DATUM_Vector of datum array
  88. | DATUM_Pair of datum * datum
  89.  
  90. (*
  91. \marktype{Syntax}{SumType}{quote}{The \quote{}s, please see \ref{subsec:syntax_quote}}
  92. *)
  93. and quote =
  94. | QUOTE_Quote of expr
  95. | QUOTE_Quasiquote of expr
  96. | QUOTE_Unquote of expr
  97. | QUOTE_Splice of expr
  98.  
  99. (*
  100. \marktype{Syntax}{SumType}{formals}{The \formal{}s, please see \ref{subsec:syntax_formals}}
  101. *)
  102. and formals =
  103. | FORMALS_IdentList of ident list
  104. | FORMALS_SoloIdent of ident
  105. | FORMALS_IdentPair of ident list * ident
  106.  
  107.  
  108. (*
  109. \marktype{Syntax}{SumType}{expr}{The \expr{}s, please see \ref{subsec:syntax_expr}}
  110. *)
  111.  
  112. and expr =
  113. | EXPR_Ident of ident
  114. | EXPR_Thunk of datum
  115. | EXPR_Call of expr * expr list
  116. | EXPR_Lambda of ident option * formals * body
  117. | EXPR_Cond of Conditional.t
  118. | EXPR_Assign of ident * expr
  119. | EXPR_Macro of Macro.t
  120. | EXPR_Include of Include.t
  121. | EXPR_Quote of quote
  122. | EXPR_Derived of DerivedExpr.t
  123.  
  124. (*
  125. Auxillary types for \modsty{Syntax} module.
  126. *)
  127. and ident = string
  128.  
  129. and label = string
  130.  
  131. and body = expr list
  132. end
  133.  
  134. (*
  135. \moduledefend{Syntax}
  136. *)
  137.  
  138. (*
  139.  
  140. \section{\moduledef{Parser}}\label{sec:mod_parser}
  141.  
  142. \subsection{A theory for \sexpr}\label{subsec:theory_sexpr}
  143.  
  144. A theory for \sexpr\ was proposed in \parencite{sato1983theory} and subsequently in \parencite{sato1985theory}.
  145.  
  146. % TODO: Discuss Sato's Sexp theory
  147.  
  148. \subsection{The challenges in parsing \scheme}\label{subsec:challenges_parse_scheme}
  149.  
  150. Parsing \sexpr{}s are pretty simple, however; \scheme\ brandishes them with its own flavor of \textit{extensibility}, meaning that, an \scheme\ program is a living thing. It's not what is written down in a file, it gets expanded and expanded during runtime. This makes a native-code \index{compiler} for \scheme\ to be quite difficult to make. \parencite{dybvig1987models} discusses three models for implementing \scheme, and seminal as the dissertation is (I have made good use of it as well, this literate program is peppered with citations of this dissertation), all three models are what we today consider \textit{interpretive}. Since \hacheme\ is a compiler and not an interpreter, it must make use of \partialeval\ to first get all the expressions into a \normalform. The \modsty{Parser} must have facilities that will be used repeatedly.
  151.  
  152. *)
  153.  
  154. (*
  155. \moduledefbegin{Parser}
  156. *)
  157.  
  158. module Parser = struct
  159.  
  160. (*
  161. \submoduledefbegin{Lexeme}
  162.  
  163. The \submodsty{Lexeme} submodule holds the \lexeme\ types for \hacheme.
  164.  
  165. \asidebox{Did you know...}{A \textit{lexeme} is like a \textit{raw token}. Just like when you put dough into oven and it bakes and becomes bread, when you put a \lexeme\ into a \gengrammar, it becomes a \token.}
  166.  
  167. *)
  168. module Lexeme = struct
  169. type t =
  170. | LEXEME_Number of string
  171. | LEXEME_Ident of string
  172. | LEXEME_String of string
  173. | LEXEME_Symbol of string
  174. | LEXEME_Char of string
  175. | LEXEME_Delim of string
  176. | LEXEME_Punct of string
  177. end
  178.  
  179.  
  180. (*
  181. \submoduledefend{Lexeme}
  182. *)
  183.  
  184. (*
  185. \markexn{End_of_consumption}{This is raised at the end of \textit{consume_\ldots} functions}
  186. *)
  187. exception End_of_consumption
  188. (*
  189. \markmodfn{Parser}{scan_hacheme}{The lexical scanner for \hacheme, please seee \ref{subsec:lexical_sanning_of_hacheme}}
  190. *)
  191.  
  192. let scan_hacheme (input : string) : Lexem.t list =
  193. let char_list = Utils.explode input in
  194.  
  195. (*
  196. \markfnfn{scan_hacheme}{scan_number}{The \index{scan_number|texttt} function will be invoked when we reach a digit}
  197. *)
  198. let scan_number (input' : char list) : (string * char list) =
  199. let buffer = Buffer.create 21 in
  200.  
  201. (*
  202. \markrecfn{scan_number}{scan_hacheme}{consume_numeric_chars}{The \recfn\ which will gobble up any numberic symbol}
  203. *)
  204.  
  205. let rec consume_numeric_chars ch =
  206. match ch with
  207. | '0' .. '9'
  208. | 'a' | 'b' | 'c' | 'd' | 'e'
  209. | 'E'
  210. | '.' ->
  211. Buffer.add_char buffer ch;
  212. consume_numeric_chars (List.tl input')
  213. | _ -> ((Buffer.contents buffer), input')
  214.  
  215. in
  216. consume_numeric_chars input'
  217. in
  218.  
  219. let
  220.  
  221.  
  222.  
  223.  
  224. end
  225.  
  226. (*
  227. \moduledefend{Parser}
  228. *)
  229.  
  230. (*
  231.  
  232. \newpage
  233. \printindex
  234.  
  235. \newpage
  236. \printglossaries
  237.  
  238. \newpage
  239. \printbibliography
  240.  
  241. \begin{filecontents}{references.bib}
  242. @phdthesis{dybvig1987models,
  243. author="R. Kent Dybvig",
  244. title="Three Implementation Models for Scheme",
  245. year=1987,
  246. school="University of North Carolina at Chapel Hill",
  247. address="Chapel Hill, NC, USA",
  248. url="https://dl.acm.org/doi/10.5555/91071",
  249. note="Ph.D. thesis"
  250. }
  251.  
  252. @techreport{scheme2013r7rs,
  253. title={Revised^7 Report on the Algorithmic Language Scheme},
  254. editor={R. Kent Dybvig and Michael Sperber and William D. Clinger and others},
  255. year={2013},
  256. journal={Scheme Language Steering Committee, Rep. R7RS},
  257. url={https://small.r7rs.org/attachment/r7rs.pdf},
  258. }
  259. @article{sato1983theory,
  260. title={Theory of symbolic expressions, I},
  261. author={Sato, Masahiko},
  262. journal={Theoretical Computer Science},
  263. volume={22},
  264. number={1-2},
  265. pages={19--55},
  266. year={1983},
  267. publisher={Elsevier}
  268. }
  269. @article{sato1985theory,
  270. title={Theory of symbolic expressions, ii},
  271. author={Sato, Masahiko},
  272. journal={Publications of the Research Institute for Mathematical Sciences},
  273. volume={21},
  274. number={3},
  275. pages={455--540},
  276. year={1985}
  277. }
  278. @inproceedings{stoyan1984early,
  279. title={Early LISP history (1956-1959)},
  280. author={Stoyan, Herbert},
  281. booktitle={Proceedings of the 1984 ACM Symposium on LISP and functional programming},
  282. pages={299--310},
  283. year={1984}
  284. }
  285. @article{stoyan1979lisp,
  286. title={LISP history},
  287. author={Stoyan, Herbert},
  288. journal={ACM Lisp Bulletin},
  289. number={3},
  290. pages={42--53},
  291. year={1979},
  292. publisher={ACM New York, NY, USA}
  293. }
  294. @inproceedings{padget1986desiderata,
  295. title={Desiderata for the standardization of LISP},
  296. author={Padget, Julian and Chailloux, J{\'e}r{\^o}me and Christaller, Thomas and DeMantaras, Ramon and Dalton, Jeff and Devin, Matthieu and Fitch, John and Krumnack, Timm and Neidl, Eugen and Papon, Eric and others},
  297. booktitle={Proceedings of the 1986 ACM conference on LISP and functional programming},
  298. pages={54--66},
  299. year={1986}
  300. }
  301. @incollection{steele1996evolution,
  302. title={The evolution of Lisp},
  303. author={Steele, Guy L and Gabriel, Richard P},
  304. booktitle={History of programming languages---II},
  305. pages={233--330},
  306. year={1996}
  307. }
  308. @book{turbak2008design,
  309. title={Design concepts in programming languages},
  310. author={Turbak, Franklyn and Gifford, David},
  311. year={2008},
  312. publisher={MIT press}
  313. }
  314. \end{filecontents}
  315.  
  316. \begin{filecontents}{hacheme-command-definitions.tex}
  317.  
  318. \newcommand\hacheme{}
  319.  
  320. \newcounter{modcnt}
  321. \setcounter{modcnt}{0}
  322. \newcommand\moduledef[1]{%
  323. \stepcounter{modcnt}\textsc{Module} \#\arabic{modcnt} -> #1
  324. }
  325.  
  326.  
  327. \end{filecontents}
  328.  
  329. \begin{filecontents}{hacheme-glossary-definitions.tex}
  330. \newglossaryentry{Term-rewriting}{
  331. name=Term-rewriting,
  332. description={}
  333. }
  334. \end{filecontents}
  335.  
  336. \begin{filecontents}{hacheme-index-definitions.tex}
  337.  
  338. \newcommand\scheme{\index{Scheme}}
  339.  
  340. \newcommand\hachemestd{\index{R7RS@$R^{7}RS$}}
  341.  
  342. \newcommand\litprogram{\index{literate program@\textit{literate program}}}
  343.  
  344. \newcommand\lisp{\index{Lisp}}
  345.  
  346. \newcommand\formeth{\index{formal method}}
  347.  
  348. \newcommand\verif{\index{verify@\textit{formally verify}}}
  349.  
  350. \end{filecontents}
  351.  
  352. \begin{filecontents}{simpletrs.sty}
  353. \NeedsTeXFormat{LaTeX2e}
  354. \ProvidesPackage{simpletrs}[2024/9/15 A simple package for typesetting Term-rewriting systems]
  355.  
  356. \RequirePackage{amsmath}
  357. \RequirePackage{xparse}
  358.  
  359.  
  360. % Simple command for basic term-rewriting rules
  361. \NewDocumentCommand{\trule}{m m}{
  362. \[ #1 \to #2 \]
  363. }
  364.  
  365. % For conditional term-rewriting rules
  366. \NewDocumentCommand{\truleif}{m m m}{
  367. \[ #1 \to #2 \quad \text{if} \quad #3 \]
  368. }
  369.  
  370. % For contextual term-rewriting rules
  371. \NewDocumentCommand{\ctrule}{m m m}{
  372. \[ #1[#2] \to #1[#3] \]
  373. }
  374.  
  375. % For named term-rewriting rules
  376. \NewDocumentCommand{\ntrule}{m m m}{
  377. \[ #1 \colon \quad #2 \to #3 \]
  378. }
  379.  
  380. \newcommand{\relarrow}{\to}
  381. \end{filecontents}
  382.  
  383. \end{document}
  384.  
  385. *)
  386.  
Advertisement
Add Comment
Please, Sign In to add comment