Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Let's make a functional language!

yhara
December 13, 2015

Let's make a functional language!

Basics of Hindley-Milner type inference

- https://github.com/yhara/rk2015orescript

yhara

December 13, 2015
Tweet

More Decks by yhara

Other Decks in Programming

Transcript

  1. Agenda 1. What is "Type Inference?" 2. Hindley-Milner type system

    3. Implementa=on • h#ps:/ /github.com/yhara/rk2015orescript RubyKaigi 2015 3
  2. Difference from JavaScript f = fn(x){ printn(x) } f(2) //→

    2 • No semicolon • s/func/on/fn/ RubyKaigi 2015 5
  3. Me and Ruby • Enumerable#lazy (Ruby 2.0~) • Note: I'm

    not a Ruby commi>er • TRICK judge • ʰRubyͰ࡞Δحົͳϓϩάϥ ϛϯάݴޠʱ(Making Esoteric Language with Ruby) RubyKaigi 2015 8
  4. What is "Type"? • Type = Group of values •

    1,2,3, ... => Integer • "a", "b", "c", ... => String RubyKaigi 2015 10
  5. What is "Type"? • Ruby has type, too! (Integer, String,...)

    • Ruby variables do not have type, though a = 1 a = "str" # ok • This is error in C int a = 1; a = "str"; // compile error! RubyKaigi 2015 11
  6. Pros of sta)c typing 1. Op%miza%on 2. Type check def

    foo(user) print user.name end foo(123) #=> NoMethodError: undefined method `name' for 123:Fixnum RubyKaigi 2015 12
  7. Cons of sta)c typing • Type annota+on? Array<Integer> ary =

    [1,2,3] ary.map{|Integer x| x.to_s } RubyKaigi 2015 13
  8. Type inference -- Haskell ary = [1,2,3] map (\x ->

    show x) ary • No type annota+ons here RubyKaigi 2015 14
  9. RECAP • Type = Group of values • Sta3c typing

    • Check type errors • Op3miza3on • Don't want to write type annota3ons => Type Inference RubyKaigi 2015 15
  10. Various "Type Inference" • C#: • var ary = [1,2,3];

    • Haskell, OCaml: • Can omit type of func8on arguments, etc. • Hindley-Milner type system RubyKaigi 2015 16
  11. What is "type system"? • System of types, of course

    :-) • Set of rules about type • Decides which type an expression will have • Decides which types are compa>ble • eg. Inheritance • Every language has its own type system RubyKaigi 2015 18
  12. What is "type system"? • Hindley-Milner type system • Haskell

    = HM + type class + ... • OCaml = HM + variant + ... • OreScript = HM (slightly modified) • Has an algorithm to reconstruct types • without any type annotaCon(!) RubyKaigi 2015 19
  13. OreScript language spec • Literal • eg. 99, true, false

    • Anonymous func6on • eg. fn(x){ x } • Variable defini6on • eg. x = 1 • eg. f = fn(x){ x } • (Note: You can't reassign variables) • Func6on call • eg. f(3) RubyKaigi 2015 20
  14. Only unary func+on is supported • Don't worry, you can

    emulate binary func5on f = fn(x, y){ ... } f(1, 2) ɹɹ↓ f = fn(x){ fn(y){ ... } } f(1)(2) RubyKaigi 2015 21
  15. Type system of OreScript • <type> is any one of

    ... • Bool • Number • <type> → <type> • eg. is_odd :: Number → Bool • Checks • Type of a and x must be the same f = fn(a){ ... } f(x) RubyKaigi 2015 22
  16. Type inference of OreScript • Given • f = fn(x){

    is_odd(x) } • is_odd :: Number → Bool • step1 Assump9on • f :: (1) → (2) • x :: (3) • step2 Equa9ons • (1) == (3), (3) == Number, (2) == Bool • step3 Resolve • (1) == Number, (2) == Bool, (3) == Number • f :: Number → Bool RubyKaigi 2015 23
  17. RECAP • Type system = set of rules on types

    • Hindley-Minler type system • Reconstruct types without annota;on • Assume, Build equa;ons, Resolve RubyKaigi 2015 24
  18. bin/ore_script #!/usr/bin/env ruby require 'ore_script' # 1. Parse tree =

    OreScript::Parser.new.parse(ARGF.read) # 2. Type check OreScript::TypeCheck.new.check(tree) # 3. Execute OreScript::Evaluator.new.eval(tree) RubyKaigi 2015 27
  19. 1. OreScript::Parser • Convert source code into a tree (parse

    tree) fn(x){ add(x)(1) } RubyKaigi 2015 28
  20. Parser library for Ruby • racc gem • treetop/parslet/citrus gem

    • Write by hand • Recursive Descent Parsing • eg. h<ps:/ /github.com/yhara/esolang-book- sources/blob/master/bolic/bolic.rb RubyKaigi 2015 29
  21. racc gem parser.ry: expression : let | function | fcall

    | if | varref | literal | '(' expression ')' let : VAR '=' expression function : 'fn' '(' params ')' '{' expressions '}' fcall : expression '(' args ')' if : 'if' '(' expression ')' '{' expressions '}' 'else' '{' expressions '}' ... RubyKaigi 2015 30
  22. Result of parsing fn(x){ add(x)(1) } [:exprs, [[:function, "x", [:exprs,

    [[:fcall, [:fcall, [:varref, "add"], [:varref, "x"]], [:literal, "Number", 1.0]]]]]]] RubyKaigi 2015 31
  23. bin/ore_script #!/usr/bin/env ruby require 'ore_script' # 1. Parse tree =

    OreScript::Parser.new.parse(ARGF.read) p tree RubyKaigi 2015 32
  24. 3. OreScript::Evaluator • Walk the tree and do what is

    expected [:if, cond_expr, then_exprs, else_exprs] def eval_if(env, cond_expr, then_exprs, else_exprs) cond = eval_expression(env, cond_expr) case cond when Value::TRUE eval_expressions(env, then_exprs) when Value::FALSE eval_expressions(env, else_exprs) else raise "must not happen" end end RubyKaigi 2015 33
  25. bin/ore_script #!/usr/bin/env ruby require 'ore_script' # 1. Parse tree =

    OreScript::Parser.new.parse(ARGF.read) # 3. Execute OreScript::Evaluator.new.eval(tree) RubyKaigi 2015 34
  26. What happens if ... f = fn(x){ add(x, 1) }

    f(true) // !? • Where's type inference? • Why we wanted type inference • "Want to check types without type ano7a8ons" RubyKaigi 2015 35
  27. 2. OreScript::TypeCheck #!/usr/bin/env ruby require 'ore_script' # 1. Parse tree

    = OreScript::Parser.new.parse(ARGF.read) # 2. Type Check (Type Inference here) OreScript::TypeCheck.new.check(tree) # 3. Execute OreScript::Evaluator.new.eval(tree) RubyKaigi 2015 36
  28. Type Inference = Type Check f = fn(x){ is_odd(x) }

    f(true) // !? • f :: (1) → (2) x :: (1) is_odd :: Number → Bool • Bool == (1) (1) == Number (2) == Bool • ∴ Bool == Number / / !? RubyKaigi 2015 37
  29. Type Inference = Type Check • Infer type before execu0ng

    program • If program has an error: • Bool == Number (unsa0sfiable) • Otherwise: • The program has consistent types (No contradic0on detected) RubyKaigi 2015 38
  30. RECAP • bin/ore_script • 1. Parse • 2. Type check

    (= Type inference) • 3. Execute RubyKaigi 2015 39
  31. Three classes for type • Type::TyRaw • A type already

    known (Number, Bool, etc.) • 99 :: #<TyRaw "Number"> • Type::TyFun • Func>on type • f :: #<TyFun #<TyRaw "Number"> -> #<TyRaw "Bool">> • Type::TyVar • A type not yet known • x :: #<TyVar (1)> • f :: #<TyVar (2)> RubyKaigi 2015 41
  32. Three steps (recap) 1. Assume types 2. Extract type equa3ons

    3. Resolve equa3ons RubyKaigi 2015 42
  33. Actual steps • 1. Assume types • 2. Extract type

    equa4ons • 3. Resolve equa4ons • 2. Extract type equa4ons • 3. Resolve equa4ons • ... RubyKaigi 2015 43
  34. OreScript::TypeCheck#infer def infer(env, node) ... end tree = Parser.new.parse("99") infer(...,

    tree) #=> [...>, Ty(Number)] tree = Parser.new.parse("f = fn(x){ add(x)(1) }")) infer(..., tree) #=> [..., Ty(Number -> Number)] RubyKaigi 2015 44
  35. OreScript::TypeCheck#infer def infer(env, node) ... when :fcall ... result_type =

    TyVar.new s1, func_type = infer(env, func_expr) s2, arg_type = infer(env.substitute(s1), arg_expr) ... equation = Equation.new( func_type, TyFun.new(arg_type, result_type) ) ... end RubyKaigi 2015 45
  36. TypeCheck.unify(*equations) • Pop one from equations • #<TyFun ty1 ->

    ty2> == #<TyFun ty3 -> ty4> • ty1 == ty3 • ty2 == ty4 • #<TyRaw "Number"> == #<TyRaw "Number"> • just ignore • #<TyVar (1)> == #<TyRaw "Number"> • Add (1) == "Number" to the answers • Replace (1) with #<TyRaw "Number"> in rest of the equations • Repeat un?l all equa?ons are removed RubyKaigi 2015 46
  37. Downside of sta-c type check • May reject "valid" program

    id = fn(x){ x } id(99) //→ 99 id(true) //→ true?? RubyKaigi 2015 48
  38. let-poly branch2 id = fn(x){ x } // id ::

    ∀(1). (1) → (1) id(99) id(true) 2 h$ps:/ /github.com/yhara/rk2015orescript/tree/let-poly RubyKaigi 2015 50
  39. let-poly branch2 id = fn(x){ x } // id ::

    ∀(1). (1) → (1) id(99) // ←id here :: (2) → (2) id(true) // ←id here :: (3) → (3) 2 h$ps:/ /github.com/yhara/rk2015orescript/tree/let-poly RubyKaigi 2015 51
  40. Acknowledgements • ʰTypes And Programming Languageʱ(TAPL) • Japanese edi7on: ʰܕγεςϜೖ໳ʱ

    • ʰϓϩάϥϛϯάݴޠͷجૅ֓೦ʱ • see also(PDF) • ʰϓϩάϥϛϯάݴޠͷجૅཧ࿦ʱ(ઈ൛) • ʰΞϧΰϦζϜWೖ໳ʱ (ಉਓࢽ) • ʰScala By Exampleʱchapter16 • Ibis (Type infrence wriJen in JavaScript) RubyKaigi 2015 52
  41. Summary • OreScript • Small language with type inference •

    Type check without type annota8on • Type inference (= type check) • Build type equea8ons • Resolve type equea8ons • hBps:/ /github.com/yhara/rk2015orescript RubyKaigi 2015 53