{"componentChunkName":"component---src-lekoarts-gatsby-theme-minimal-blog-core-templates-post-query-tsx","path":"/inductive-data-types","result":{"data":{"post":{"slug":"/inductive-data-types","title":"Inductive Data Types","date":"January 21, 2019","tags":[{"name":"Functional Programming","slug":"functional-programming"}],"description":null,"body":"function _extends() { _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); }\n\nfunction _objectWithoutProperties(source, excluded) { if (source == null) return {}; var target = _objectWithoutPropertiesLoose(source, excluded); var key, i; if (Object.getOwnPropertySymbols) { var sourceSymbolKeys = Object.getOwnPropertySymbols(source); for (i = 0; i < sourceSymbolKeys.length; i++) { key = sourceSymbolKeys[i]; if (excluded.indexOf(key) >= 0) continue; if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue; target[key] = source[key]; } } return target; }\n\nfunction _objectWithoutPropertiesLoose(source, excluded) { if (source == null) return {}; var target = {}; var sourceKeys = Object.keys(source); var key, i; for (i = 0; i < sourceKeys.length; i++) { key = sourceKeys[i]; if (excluded.indexOf(key) >= 0) continue; target[key] = source[key]; } return target; }\n\n/* @jsx mdx */\nvar _frontmatter = {\n  \"title\": \"Inductive Data Types\",\n  \"date\": \"2019-01-21T00:00:00.000Z\",\n  \"tags\": [\"Functional Programming\"],\n  \"excerpt\": \"Tree as an example\"\n};\n\nvar makeShortcode = function makeShortcode(name) {\n  return function MDXDefaultShortcode(props) {\n    console.warn(\"Component \" + name + \" was not imported, exported, or provided by MDXProvider as global scope\");\n    return mdx(\"div\", props);\n  };\n};\n\nvar layoutProps = {\n  _frontmatter: _frontmatter\n};\nvar MDXLayout = \"wrapper\";\nreturn function MDXContent(_ref) {\n  var components = _ref.components,\n      props = _objectWithoutProperties(_ref, [\"components\"]);\n\n  return mdx(MDXLayout, _extends({}, layoutProps, props, {\n    components: components,\n    mdxType: \"MDXLayout\"\n  }), mdx(\"p\", null, \"Lecture notes for COMP302.\"), mdx(\"h3\", null, \"Lists as inductive structures\"), mdx(\"ul\", null, mdx(\"li\", {\n    parentName: \"ul\"\n  }, \"NOTE: Induction works if and only if the system has a well-founded order.\")), mdx(\"h3\", null, \"Inductive definitions of data types\"), mdx(\"p\", null, \"Let \", mdx(\"inlineCode\", {\n    parentName: \"p\"\n  }, \"[ ]\"), \" is a list. If you have a list, say \", mdx(\"inlineCode\", {\n    parentName: \"p\"\n  }, \"l\"), \", and an item, say\", mdx(\"inlineCode\", {\n    parentName: \"p\"\n  }, \"i\"), \", then \", mdx(\"inlineCode\", {\n    parentName: \"p\"\n  }, \"i::l\"), \" is also a list.\\n\", mdx(\"inlineCode\", {\n    parentName: \"p\"\n  }, \"List0 = [ ]\"), \"\\n\", mdx(\"inlineCode\", {\n    parentName: \"p\"\n  }, \"List1 = [1], [2],...,[ ]\"), \"\\n\", mdx(\"inlineCode\", {\n    parentName: \"p\"\n  }, \"List2 = [1; 1], [1; 2],...,[4; 1],...\"), \"\\nThe entire collection LIST = union_n LIST_n, an infinite set. However, the description is short, concise, and finite.\\nThis is a built-in type. There are functions like \", mdx(\"inlineCode\", {\n    parentName: \"p\"\n  }, \"map, filter\"), \" in this module.\"), mdx(\"h3\", null, \"How do I define my own inductive types?\"), mdx(\"p\", null, \"Lists are linear data types, tail recursion works well for linear data types.\"), mdx(\"h4\", null, \"Trees\"), mdx(\"p\", null, \"Trees are \", mdx(\"strong\", {\n    parentName: \"p\"\n  }, \"not built-in\"), \". Here we only talked about binary trees.\"), mdx(\"ul\", null, mdx(\"li\", {\n    parentName: \"ul\"\n  }, mdx(\"p\", {\n    parentName: \"li\"\n  }, \"Inductive definition:\\n\", mdx(\"inlineCode\", {\n    parentName: \"p\"\n  }, \"EMPTY\"), \" is a tree.\\nIf \", mdx(\"inlineCode\", {\n    parentName: \"p\"\n  }, \"t1, t2\"), \" are trees, and \", mdx(\"inlineCode\", {\n    parentName: \"p\"\n  }, \"i\"), \" is an item. Then \", mdx(\"inlineCode\", {\n    parentName: \"p\"\n  }, \"i(t1, t2)\"), \" is a tree (NOT linear).\")), mdx(\"li\", {\n    parentName: \"ul\"\n  }, mdx(\"p\", {\n    parentName: \"li\"\n  }, \"How to code a \", mdx(\"inlineCode\", {\n    parentName: \"p\"\n  }, \"Tree\"), \" in OCaml\\nWe have user-defined inductive types.\\n\", mdx(\"inlineCode\", {\n    parentName: \"p\"\n  }, \"type 'a tree = Empty | Node of 'a tree * 'a * 'a tree\")), mdx(\"ol\", {\n    parentName: \"li\"\n  }, mdx(\"li\", {\n    parentName: \"ol\"\n  }, mdx(\"p\", {\n    parentName: \"li\"\n  }, mdx(\"inlineCode\", {\n    parentName: \"p\"\n  }, \"'a means:\"), \"Polymorphic binary trees. \", mdx(\"inlineCode\", {\n    parentName: \"p\"\n  }, \"'a\"), \" can be instantiated with any type: ints, strings\\u2026\"), mdx(\"p\", {\n    parentName: \"li\"\n  }, \"You don\\u2019t have to declare types. Type inference comes to help.\"), mdx(\"p\", {\n    parentName: \"li\"\n  }, mdx(\"inlineCode\", {\n    parentName: \"p\"\n  }, \"let max (n, m) = if n < m then m else n;;\"))), mdx(\"li\", {\n    parentName: \"ol\"\n  }, mdx(\"p\", {\n    parentName: \"li\"\n  }, mdx(\"inlineCode\", {\n    parentName: \"p\"\n  }, \"Node\"), \": constructor function. Always start with capital letters.\"))), mdx(\"ul\", {\n    parentName: \"li\"\n  }, mdx(\"li\", {\n    parentName: \"ul\"\n  }, mdx(\"p\", {\n    parentName: \"li\"\n  }, \"Examples:\"), mdx(\"pre\", {\n    parentName: \"li\"\n  }, mdx(\"code\", _extends({\n    parentName: \"pre\"\n  }, {\n    \"className\": \"language-ocaml\"\n  }), \"let t1 = Node(Empty, 0, (Node(Empty, 1, Empty)));;\\n\\n(* height *)\\nlet rec height (t: 'a tree) =\\n    match t with\\n    | Empty -> 0\\n    | Node (l,_,r) -> 1 + max(height l, height r);;\\n\\n(* sumNodes *)\\n(* showInt*)\\n(* inOrder *)\\n(* (inOrder l); (showInt n); (inOrder r);;\\n    The ; suggests sequence. It's not pure.\\n*)\\n(* flatten *)\\n\"))))), mdx(\"li\", {\n    parentName: \"ul\"\n  }, mdx(\"p\", {\n    parentName: \"li\"\n  }, \"Expression trees\"), mdx(\"p\", {\n    parentName: \"li\"\n  }, mdx(\"inlineCode\", {\n    parentName: \"p\"\n  }, \"Const, Var, Plus, Times\")), mdx(\"p\", {\n    parentName: \"li\"\n  }, mdx(\"inlineCode\", {\n    parentName: \"p\"\n  }, \"type binding\"), \": char \", \"*\", \" int\"), mdx(\"p\", {\n    parentName: \"li\"\n  }, mdx(\"inlineCode\", {\n    parentName: \"p\"\n  }, \"type env = binding list\")), mdx(\"p\", {\n    parentName: \"li\"\n  }, mdx(\"inlineCode\", {\n    parentName: \"p\"\n  }, \"let rho: env = [('x', 11); ('y', 7); ('z', 2)]\")), mdx(\"p\", {\n    parentName: \"li\"\n  }, \"What if there\\u2019s no such names?\"), mdx(\"ul\", {\n    parentName: \"li\"\n  }, mdx(\"li\", {\n    parentName: \"ul\"\n  }, mdx(\"p\", {\n    parentName: \"li\"\n  }, \"OPTION TYPE, a new type constructor. If \", mdx(\"inlineCode\", {\n    parentName: \"p\"\n  }, \"t\"), \" is a type, then \", mdx(\"inlineCode\", {\n    parentName: \"p\"\n  }, \"t option\"), \" is another type. Allows you to optionally return a \", mdx(\"inlineCode\", {\n    parentName: \"p\"\n  }, \"NOne\"), \" value.\")), mdx(\"li\", {\n    parentName: \"ul\"\n  }, mdx(\"p\", {\n    parentName: \"li\"\n  }, \"EX. 1729 is an integer.\"), mdx(\"p\", {\n    parentName: \"li\"\n  }, \"None is an integer option.\"), mdx(\"p\", {\n    parentName: \"li\"\n  }, \"Some 1729 is an integer option.\"), mdx(\"p\", {\n    parentName: \"li\"\n  }, \"Not returning int value, return \", mdx(\"inlineCode\", {\n    parentName: \"p\"\n  }, \"int option\"), \".\"))))), mdx(\"pre\", null, mdx(\"code\", _extends({\n    parentName: \"pre\"\n  }, {\n    \"className\": \"language-ocaml\"\n  }), \"let rec eval (e: exptree) (rho: env) =\\n    match e with\\n    | Const n -> n\\n    | Var v -> (match (lookup v rho)) with\\n                | None -> raise NotFound\\n                | Some r -> r\\n\")));\n}\n;\nMDXContent.isMDXComponent = true;","excerpt":"Tree as an example","banner":null}},"pageContext":{"slug":"/inductive-data-types"}}}