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

Lens : Smart setter for immutable data

Lens : Smart setter for immutable data

A implementation of lens library in Perl

Masahiro Honma

August 29, 2014
Tweet

More Decks by Masahiro Honma

Other Decks in Technology

Transcript

  1. 2014೥8݄29೔
    Lens : Smart setter for immutable data
    hiratara @ FreakOut

    View Slide

  2. immutable data
    package Talk;
    use Moo;
    has speaker => (
    is => ’ro’, requires => 1
    );
    has name => (
    is => ’ro’, requires => 1
    );

    View Slide

  3. Nested immutable data
    getter works well
    $schedule->talk->speaker->name;

    View Slide

  4. Nested immutable data
    setter won’t work well
    # Can’t write it
    $new_schedule =
    $schedule->talk->speaker
    ->name("hiratara");

    View Slide

  5. Nested immutable data
    Must copy and copy and copy ...
    $new_speaker = Speaker->new(
    name => "hiratara",
    age => $speaker->age,
    mail => $speaker->age,
    );
    $new_talk = Talk->new(
    title => $talk->title,
    speaker => $new_speaker,
    );
    $new_schedule = Schedule->new(
    talk => $new_talk,
    kind => $schedule->kind,
    );

    View Slide

  6. Ugly way to solve it
    Though, I hate mutable data.
    package Talk;
    use Moo;
    has speaker => (
    is => ’rw’, requires => 1
    );
    has name => (
    is => ’rw’, requires => 1
    );

    View Slide

  7. Ugly way to solve it

    View Slide

  8. Another way : use Lens
    Lens acts like an accessor
    # getter (will get ’hiratara’)
    (talk . speaker . name)->($schedule);
    # setter
    $new_schedule =
    (talk . speaker . name)->($schedule, ’hiratara’);

    View Slide

  9. What is Lens?
    • A library of Haskell
    • https://github.com/ekmett/lens/

    View Slide

  10. Let’s implement Lens
    Lens is “costate comonad coalgebra”. We
    have only to implement it.

    View Slide

  11. Let’s implement Lens
    Lens is “costate comonad coalgebra”. We
    have only to implement it.

    View Slide

  12. Category theory
    Send e-mail to [email protected] if
    you want to buy this

    View Slide

  13. What is co-
    co- means opposite or dual.

    View Slide

  14. This is monad
    M
    ηM //
    D
    D
    D
    D
    D
    D
    D
    D
    D
    D
    D
    D
    D
    D
    D
    D MM
    µ

    M

    oo
    zzzzzzzz
    zzzzzzzz
    M
    MMM
    Mµ //
    µM

    MM
    µ

    MM
    µ
    // M

    View Slide

  15. comonad is a dual of monad
    W WW
    ϵW
    oo Wϵ // W
    W
    EEEEEEEE
    EEEEEEEE δ
    OO y
    y
    y
    y
    y
    y
    y
    y
    y
    y
    y
    y
    y
    y
    y
    y
    WWW WW

    oo
    WW
    δW
    OO
    W
    δ
    oo
    δ
    OO

    View Slide

  16. comonad in Perl
    package Comonad;
    sub counit;
    sub coflatten;
    sub map;
    sub coflat_map;

    View Slide

  17. The type of state monad
    (A × S)S

    View Slide

  18. The type of state monad
    (A × S)S ≃ (−S ◦ (− × S))(A)

    View Slide

  19. costate comonad
    The dual of state monad
    AS × S ≃ ((− × S) ◦ −S)(A)

    View Slide

  20. costate comonad in Perl
    package Comonad::Costate;
    use Moo;
    extends ’Comonad’;
    has func => (is => ’ro’);
    has state => (is => ’ro’);
    sub counit my $self = shift; $self->func

    View Slide

  21. costate comonad in Perl
    sub map {
    my ($self, $f) = @_;
    (ref $self)->new(
    func => _comp($f, $self->func),
    state => $self->state,
    );
    }

    View Slide

  22. costate comonad in Perl
    sub coflatten {
    my $self = shift;
    my $class = ref $self;
    my $self_func = $self->func;
    $class->new(
    func => sub {
    my $state = shift;
    $class->new(
    func => $self_func,
    state => $state,
    );
    },
    state => $self->state,
    );
    }

    View Slide

  23. algebra of monad M
    MMX Mh //
    µX

    MX
    h

    MX h // X
    X
    ηX //
    C
    C
    C
    C
    C
    C
    C
    C
    C
    C
    C
    C
    C
    C
    C
    C MX
    h

    X

    View Slide

  24. coalgebra of comonad W
    WWX WX
    Wh
    oo
    WX
    δX
    OO
    X
    h
    oo
    h
    OO X
    C
    C
    C
    C
    C
    C
    C
    C
    C
    C
    C
    C
    C
    C
    C
    C WX
    ϵX
    oo
    X
    h
    OO

    View Slide

  25. costate comonad coalgebra in Perl
    coalgebra => sub {
    my $data = shift;
    Comonad::Costate->new(
    func => sub {
    my $value = shift;
    (ref $data)->new(
    %$data,
    $field => $value,
    )
    },
    state => $data->$field,
    );
    },

    View Slide

  26. costate comonad coalgebra
    costate comonad coalgebra is the
    combination of setter and getter. This is Lens.
    A → (AS × S) ≃ (A → AS) × (A → S)
    • A → AS is setter
    • A → S is getter

    View Slide

  27. composition of Lenses
    package Lens;
    use overload (
    ’.’ => ’chain’, fallback => 1,
    );
    sub chain {
    my ($self, $other) = @_;
    Lens->new(
    coalgebra => sub {
    ...
    },
    );
    }

    View Slide

  28. Field lens
    sub lens ($) {
    my $field = shift;
    Lens->new(
    coalgebra => sub {
    my $data = shift;
    Comonad::Costate->new(
    func => sub {
    my $v = shift;
    (ref $data)->new(%$data, $field => $v)
    },
    state => $data->$field,
    );
    }
    );
    }

    View Slide

  29. Lens is more powerful
    $schedule =
    (talk . speaker . name)->($schedule, ’hiratara’);
    # Setter and Getter of a part of string
    $schedule =
    (talk . speaker . name . substr_lens(3, 2))
    ->($schedule, ’@’);
    # $schedule->talk->speaker->name will be "[email protected]"

    View Slide

  30. CONCLUSION
    • Lens is a good setter for immutable data
    • https://github.com/hiratara/p5-Lens
    To tell the truth, haskell’s lens implementation
    has the type:
    Let (−(V))V, −(S): SetSet → Set be functors
    (−(V))V ⇒ −(S)

    View Slide