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

extensible_interpreter_in_ML

 extensible_interpreter_in_ML

ML Day #1

takahisa watanabe

March 31, 2018
Tweet

Other Decks in Science

Transcript

  1. &YQSFTTJPO1SPCMFN ੩తܕ෇͖ͷϓϩάϥϜݴޠͰ  ιʔείʔυΛʮฤूͤͣʯʹσʔλܕ΍खଓ͖Λ௥ՃͰ͖Δ͔  3 The Expression Problem is

    a new name for an old problem. The goal is to define a datatype by cases, where one can add new cases to the datatype and new functions over the datatype, without recompiling existing code, and while retaining static type safety (e.g., no casts). http://homepages.inf.ed.ac.uk/wadler/papers/expression/expression.txt Expression Problem Extensibility
  2. ֦ுՄೳΠϯλϓϦλ FYUFOTJCMF JOUFSQSFUFS l ΠϯλϓϦλ։ൃʹ͓͚Δ&YQSFTTJPO1SPCMFNͷղ 4 ධՁث ऴλάϨε๏ 0QFOSFDVSTJPO 0CKFDUBMHFCSB

    ߏจղੳث ཱ֬͞Εͨख๏͸ͳ͍ ߏจղੳثੜ੒ܥΛ࢖͏ͱ͍͏खஈ͸͋Δ͕ ࠓճͷٞ࿦ͷର৅ʹ͸ؚΊͳ͍
  3. ֦ுՄೳΠϯλϓϦλ 9 l ର৅ݴޠΛ੩తʹ֦ுՄೳͳΠϯλϓϦλ lର৅ݴޠͷ֦ுΛͲ͏΍ͬͯදݱ͢Δ   type e =

    Lam of string * e | App of e * e | Var of string | Int of int ௥Ճ 0$BNMʹ͸ͦͷͨΊͷػೳ͕͋Δ
  4. &YUFOTJCMFWBSJBOUUZQFT l։͔Εͨz୅਺తσʔλܕ ü ίϯετϥΫλΛޙ͔Β௥ՃՄೳ üྫ֎ܕexnͷҰൠԽ એݴ type t = ..

    ίϯετϥΫλͷ௥Ճ ݱࡏͷείʔϓͰͷΈ༗ޮʹͳΔ  type t += A type t += B of int | C 10
  5. ྫ୊ type e = .. type e += Int of

    int | Add of e * e let rec eval = function | Int n -> n | Add (n, m) -> eval n + eval m 13
  6. ྫ୊खଓ͖ͷ௥Ճ type e = .. type e += Int of

    int | Add of e * e let rec eval = function | Int n -> n | Add (n, m) -> eval n + eval m let rec show = function | Int n -> sprintf “%d” n | Add (n, m) -> sprintf “(%s+%s)” (show n) (show m) 14
  7. ྫ୊σʔλܕͷ௥Ճ type e = .. type e += Int of

    int | Add of e * e type e += Sub of e * e let rec eval = function | Int n -> n | Add (n, m) -> eval n + eval m | Sub (n, m) -> eval n – eval m let rec show = function | Int n -> sprintf “%d” n | Add (n, m) -> sprintf “(%s + %s)” (show n) (show m) | Sub (n, m) -> sprintf “(%s - %s)” (show n) (show m) 15 ੺ࣈͷ෦෼͸ιʔείʔυͷ ฤूʹΑΔରԠ͕ඞཁ
  8. ΍Γ͍ͨ͜ͱ 16 let rec eval = function | Int n

    -> n | Add (n, m) -> eval n + eval m let rec eval_sub = function | Sub (n, m) -> eval_sub n – eval_sub m | e -> eval e • ֦ுͨ͠෦෼͚ͩΛॲཧ͢Δखଓ͖Λ௥Ճ • ֦ுͨ͠෦෼Ҏ֎ͷॲཧ͸طଘͷखଓ͖ʹॲཧΛҕৡ ҕৡ
  9. ໰୊ 17 let rec eval = function | Int n

    -> n | Add (n, m) -> eval n + eval m let rec eval_sub = function | Sub (n, m) -> eval_sub n – eval_sub m | e -> eval e • evalؔ਺ͷ࠶ؼݺͼग़͠Ҏ߱, eval_subؔ਺͕ݺ͹Εͳ͍ • ໰୊ͱͳΔྫAdd (Sub (Int 1, Int 2), Int 3) ҕৡ
  10. ྫ୊΁ͷద༻ let eval_open = fun eval -> | Int n

    -> n | Add (n, m) -> eval n + eval m let eval_sub_open = fun eval -> | Sub (n, m) -> eval n – eval m | e -> eval_open eval e 19
  11. 5ZJOHUIFLOPU l࠷ޙʹ։͔Εͨ࠶ؼؔ਺Λlด͡Δzఆ͕ٛඞཁ 20 let rec eval e = eval_sub_open eval

    e PQFOSFDVSTJPOؔ਺ͷ࠶ؼݺͼग़͕͠ શͯ͜ͷevalΛࢦ͢Α͏ʹͳΔ
  12. ྫ୊ΫϥεʹΑΔ࣮ݱ 23 class add_or_int = object (this) method eval =

    function | Int n -> n | Add (n, m) -> this#eval n + this#eval m end class sub = object (this) inherit add_or_sub as super method eval = function | Sub (n, m) -> this#eval n – this#eval m | e -> super#eval e end ݱࡏͷखଓ͖ͰΧόʔ͠ͳ͍ཁૉ͸਌ʹॲཧΛҕৡ
  13. finale : *OWFSUJCMF4ZOUBYGPS.- 31 http://github.com/takahisa/finale ߏจղੳثͱϓϦϯλΛಉ࣌ʹఆٛՄೳ "QQMJDBUJWF1BSTFSDPNCJOBUPSͬΆ͍ελΠϧ let jboolean =

    jbooleanE <$> (text "true" *> pure (=) true) <|> jbooleanE <$> (text "false" *> pure (=) false) ྫ+40/ਅِ஋Ϧςϥϧͷߏจఆٛ • ऴλάϨε๏ʹΑΔܕ҆શͳݴޠ಺%4-ͱ࣮ͯ͠૷ • ϞδϡϥʔͳߏจղੳϓϦϯτͷͨΊͷػೳΛఏڙ
  14. Ϟδϡϥʔͳߏจղੳʹ޲͚ͯ lύʔαϓϦϯλͷ֦ு๏͸લ൒ͱಉ͡ lͨͩ͠ lਖ਼͍݁͠ՌzΛಘΔͨΊʹ͸޻෉͕ඞཁ   32 • Modular parsers

    should support left-recursion. • Modular parsers should use a longest match composition operator 5ZQF4BGF.PEVMBS1BSTJOH<;IBOH >  • ࠨ࠶ؼ • ࠷௕Ұக
  15. Ϟδϡϥʔͳߏจղੳͷ໰୊ 34 ॱ൪ʹؔ͢Δ໰୊ FYQ JOU Լʹ௥Ճ نଇͷ௥ՃΛߦ͏ͱʜ FYQ JOU c

    JOU b `FYQ FYQ JOU b `FYQ cJOU ্ʹ௥Ճ Լͷنଇʹ ӬԕʹભҠ͠ͳ͍