{"id":18786555,"url":"https://github.com/fausto95/sml","last_synced_at":"2026-02-19T14:32:27.686Z","repository":{"id":89907543,"uuid":"207002510","full_name":"Fausto95/sml","owner":"Fausto95","description":"Notes on Standard ML","archived":false,"fork":false,"pushed_at":"2019-09-28T12:05:48.000Z","size":12,"stargazers_count":2,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-10-22T03:27:41.090Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","language":"Standard ML","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/Fausto95.png","metadata":{"files":{"readme":"Readme.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2019-09-07T17:28:12.000Z","updated_at":"2023-03-08T16:51:24.000Z","dependencies_parsed_at":"2023-05-30T11:00:23.720Z","dependency_job_id":null,"html_url":"https://github.com/Fausto95/sml","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/Fausto95/sml","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Fausto95%2Fsml","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Fausto95%2Fsml/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Fausto95%2Fsml/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Fausto95%2Fsml/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/Fausto95","download_url":"https://codeload.github.com/Fausto95/sml/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Fausto95%2Fsml/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":29618332,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-02-19T13:04:20.082Z","status":"ssl_error","status_checked_at":"2026-02-19T13:03:33.775Z","response_time":117,"last_error":"SSL_connect returned=1 errno=0 peeraddr=140.82.121.6:443 state=error: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-11-07T20:51:53.171Z","updated_at":"2026-02-19T14:32:27.668Z","avatar_url":"https://github.com/Fausto95.png","language":"Standard ML","funding_links":[],"categories":[],"sub_categories":[],"readme":"# These are my personal notes that I'm taking while learning Standard ML\n\n\n### REPL\nhttps://www.smlnj.org/doc/interact.html\n\n#### launching the repl\n\n```bash\nsml\n```\nTo use a file in the repl, simply:\n\n```\nuse \"filename.sml\";\n```\n\n----\n\n### Variables Bindings\n\n```sml\nval a = 2;\n```\n\nSyntax:\n  - Keyword \u003cb\u003eval\u003c/b\u003e and punctuation \u003cb\u003e=\u003c/b\u003e and \u003cb\u003e;\u003c/b\u003e\n  - Variable \u003cb\u003ea\u003c/b\u003e\n  - Expression e\n    - Many forms of these, most containing subexpressions\n\n- Syntax is just how you write something\n\n- Semantics is what that something means\n  - Type-checking(before program runs)\n  - Evaluation(as program runs)\n\n- For variable bindings:\n  - Type-check expression and extends static environment\n  - Evaluate expression and extend dynamic environment\n\n---------\n\n\n### Conditional Expressions\n\n```sml\n  if y = 0\n  then 1\n  else x\n```\n\nSyntax:\n  if e1 then e2 else e3\n  where if, then, and else are keywords and e1, e2 and e3 are subexpressions\n\nType-checking:\n  first e1 must have type bool\n  e2 and e3 can have any typee (let's call it t), but they\n  must have the same type t\n  the type of the entire expression is also t\n\n---------\n\n### Functions\n\nFunctions are created using the keyword `fun`\n\n```sml\nfun pow(x: int, y: int) =\n  if y = 0\n  then 1\n  else x * pow(x, y - 1)\n```\n\n```sml\npow(2, 3);\n```\nUnless a function has one argument, we need to use parentheses to call it\n\n---------\n\n### Tuples\n\n\nTuples are created using parenthesis `(24, \"Faustino\")`\n\n```sml\nval tuples = (24, \"Faustino\")\n```\n\nAccessing tuple elements\n\n```sml\n#2 tuples;\n```\n\n```sml\nfun swap(pr: int * bool ) =\n  (#2 pr, #1 pr)\n```\n```sml\nswap(2, true)\n```\n\n---------\n\n### Lists\n\nLists are declared using brackers `[]`. Lists elements must have the same type\n\n\n```sml\nval list = [2, 3, 4]\n```\n\n\n---------\n\n\n### Let Expressions(Scope)\n\n - Syntax\n\n```sml\nlet b1 b2 ... bn in e end\n```\n\nEach \u003cb\u003ebi\u003c/b\u003e is any binding and \u003cb\u003ee\u003c/b\u003e is any expression(body)\n\n```sml\nfun silly1(z: int) =\n  let\n    val x = if z \u003e 0 then z else 34\n    val y = x + z + 9\n  in\n    if x \u003e y then x * 2 else y * y\n  end\n```\n\n---------\n\n### Nested Functions\n\n```sml\nfun countup_from1_3(x: int) =\n  let\n    fun count(from: int) =\n      if from = x\n      then x::[]\n      else from :: count(from + 1)\n  in\n    count(1)\n  end\n```\n\n---------\n\n### Options\n\n - t option is a type for any type t\n  - (much like t list, but different type, not a list)\n\nBuilding:\n  - NONE has type 'a(alpha) option (much like [] has type 'a list)\n  - SOME e has type t option if e has type t (much like e::[])\n\nAccessing:\n  - isSome has type 'a option -\u003e bool\n  - valOf has type 'a option -\u003e 'a (exception if given NONE)\n\n\n---------\n\n### Boolean Operations\n\ne1 `andalso` e2\ne1 `orelse` e2\n\n`not` e1\n\nEquivalents\n\n\n\n```sml\n(* el andalso e2 *)\nif e1\nthen 2\nelse false\n```\n\n```sml\n(* el orelse e2 *)\nif e1\nthen true\nelse e2\n```\n\n```sml\n(* not e1 *)\nif e1\nthen false\nelse true\n```\n\n---------\n\n### Comparisons\n\n- Equality `=`\n- Not Equal `\u003c\u003e`\n- Greater than `\u003e`\n- Less than `\u003c`\n- Greater than or equal `\u003e=`\n- Less than or equal `\u003c=`\n\n\u003e '\u003e' \u003c \u003e= \u003c= can be used with real(float), but not 1 int and 1 real(float)\n\n\u003e = \u003c\u003e can be used with any \"equality type\" but not with real(float)\n\n\n---------\n\n### 2nd Week\n\n### Building bigger types\n\nBase types are:\n\n- int\n- bool\n- unit\n- char\n\nWays to build compound types: tuples, lists, options.\n\n\n3 most important type building-blocks in any language\n\n- \"Each of\": A `t` value contains values of each of `t1` `t2` .. `tn`\n- \"One of\": A `t` value contains values of one of `t1` `t2` .. `tn`\n- \"Self reference\": A `t` value can refer to other `t` values\n\nCompound types, are ways that you build new types with other types inside of them.\n\n----\n\nWhich of the following concepts would be appropriate to represent using just an \"each of\" type?\n- [x] The position of an object in 3-space\n  \u003e The position of an object can easily be represented by each of an x, y, and z coordinates. Likewise, the first and last names of a person can be easily represented as each of the first and last names. However, the status of a printer is either printing, or ready, or jammed, or on fire, etc, which doesn't fit well into an each of type. Finally, the length of a list is just simply a count, and doesn't need an each of type.\n- [ ] Isso deve ser selecionado\n\n- [ ] The status of a printer (printing, ready, jammed, on fire, etc...)\n\n- [ ] Não selecionado está correto\n\n- [ ] The length of a list\n\n- [ ] ão selecionado está correto\n\n- [x] The first and last names of a person\n  \u003e The position of an object can easily be represented by each of an x, y, and z coordinates. Likewise, the first and last names of a person can be easily represented as each of the first and last names. However, the status of a printer is either printing, or ready, or jammed, or on fire, etc, which doesn't fit well into an each of type. Finally, the length of a list is just simply a count, and doesn't need an each of type.\n\n----\n\nWhich of the following concepts would be appropriate to represent using just a \"one of\" type?\n\n- [ ] The particular flavor of ice cream in a bowl (assuming, of course, that bowls only hold one flavor of ice cream)\n  \u003eA bowl of ice cream has one of the many possible flavors. Likewise, the current weather is one of many possible values. However, the set of students that showed up to class is not easily represented by a \"one of\" type, and the current rabbit population needs nothing more than a simple count for representation.\n\n\n- [ ] The set of students who actually showed up to class today\n- [x] The current weather (sunny, partly cloudy, rainy, hurricane, etc...)\n  \u003e A bowl of ice cream has one of the many possible flavors. Likewise, the current weather is one of many possible values. However, the set of students that showed up to class is not easily represented by a \"one of\" type, and the current rabbit population needs nothing more than a simple count for representation.\n\n- [ ] The current rabbit population in a park\n\n\n\n----\n\nExamples\n\n  * Tuples build each-of types\n    - `int` * `bool` contains an `int` and a `bool`\n  * Options build one-of types\n    - `int option` contains an `int` or it contains no data\n  * Lists use all three building blocks\n    - `int list` contains an `int` and another `int list` or it contains no data\n  * And of course we can nest compound types\n    - `((int * int) option * (int list list)) option`\n\n----\n\n\nAnother way to build each-of types in ML:\n  - Recods: have named fields and indexes\n  - Connection to tuples and idea of syntactic sugar\n\n\n### Records\n\n```sml\nval x = {\n  bar = (1 + 2, true andalso true) ,\n  foo = 3 + 4,\n  baz = (false, 9)\n}\n\nval me = {age=24, name=\"Faustino\"} : {age: int, name: string}\n\n(* Accessing records elements *)\n#name me\n```\nTuples as Syntactic Sugar\n\n```sml\nval me = {age=24, name=\"Faustino\"} : {age: int, name: string}\n\nval a_pair = (5 + 5, 3 + 3)\n\nval a_record = {second = 2 + 2, first = 2 + 1} (* first:int, second: int *)\nval b_pair = {2 = 5, 1 = 6} (* (int * int) *) (* (6, 5) *)\n\n#1 a_pair + 2 b_pair (* 9: int *)\n\nval x = {3 = \"hi\", 1 = true, 2 = 3 + 2} (* (true, 5, \"hi\") *)\n\n```\n\n----\n\n\n### Data Types Bindings\n\nTag: Constructor\nData: the corresponding data part\n\n```sml\ndatatype my_own_type = TwoInts of int * int\n  | Str of string\n  | Pizza\n\nval a = Str \"hi\"\nval b = Str\nval c = Pizza\nval d = TwoInts(2 + 1, 3)\nval e = a\n```\n\n### Case Expressions\n\nConcept to access made up from the types we introduced with data type bindings\n\nExample:\n\n```sml\nfun f x = (* f has type my_own_type -\u003e int*)\n  case x of\n    Pizza =\u003e 3\n  | TwoInts(i1, i2) =\u003e i1 + i2\n  | Str s =\u003e String.size s\n```\n\n\n#### Expression Trees\n\n```sml\ndatatype exp = Constant of int\n  | Negate of exp\n  | Add of exp * exp\n  | Multiply exp * exp\n```\n\nAn expression in ML of type:\n`Add (Constant (10 + 9), Negate (Constant 4)`\n\nPicturing the resulting value:\n\n```\n            Add\n    Constant    Negate\n      19       Constant\n                  4\n```\n\n\n### Pattern Matching\n\n```sml\nfun max_constant3 e =\n  let fun max_of_two(e1, e2) = Int.max(max_constant3 e1, max_constant3 e2)\n  in\n  case e of\n    Constant i =\u003e i\n  | Negate e2 =\u003e max_constant3 e2\n  | Add(e1, e2) =\u003e max_of_two(e1, e2)\n  | Multiply(e1, e2) =\u003e max_of_two(e1, e2)\n  end\n```\n\n\n### Recursive Datatypes\n\n```sml\n(* Options *)\n\nfun inc_or_zero intoption =\n  case intoption of\n    NONE =\u003e 0\n    | SOME i =\u003e i + 1\n```\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ffausto95%2Fsml","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ffausto95%2Fsml","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ffausto95%2Fsml/lists"}