Contravariant Functors: The Other Side of the Coin

schedule May 22nd 10:30 - 11:00 AM place Green Room people 167 Interested

Since their introduction, Applicative and its sidekick Alternative have become ubiquitous and beloved type classes in Haskell and similar languages such as Purescript. By contrast, their contravariant counterparts are relatively unknown. These contravariant structures are useful, and their time has come!

In this talk, we will learn about Contravariant, and its subclasses Divisible and Decidable. How do they work? How do they relate to the classes we know? Are they useful? For what? This talks aims to demystify these classes in an approachable way, by demonstrating their use with practical motivating examples.

 
2 favorite thumb_down thumb_up 0 comments visibility_off  Remove from Watchlist visibility  Add to Watchlist
 

Outline/Structure of the Talk

The talk will begin with a brief review of the Functor, Applicative, and Alternative type classes in Haskell. These will be demonstrated with examples from the Trifecta parser combinator library or similar. These examples will highlight how (<*>) can deal with products while (<|>) can deal with sums.

The Contravariant type class will be introduced. An example will be given using the Predicate type. The question will be posed: Is there a contravariant equivalent to Applicative? The answer is yes! We will introduce the Divisible class and show that Predicate is an instance. Asking the same question of Alternative, we find Decidable, and see how Predicate is an instance. Thinking back to the parser example from earlier, we now look at a pretty-printer library. Looking at the venerable ansi-wl-pprint, we see how it could be given a Divisible and Decidable interface and the compositional benefits gained by using this vocabulary for pretty-printing. The sv library will be mentioned, which uses a similar contravariant interface for encoding Haskell data-types as CSV files.

The talk will end with a mention of how the discrimination library uses Divisible and Decidable to define a compositional vocabulary for fast sorting and grouping, and encouraging attendees to watch Edward Kmett's 2014 Lambda Jam keynote for more information on that.

Learning Outcome

An attendee should learn what "contravariant" means with respect to an algebraic data type. They should gain an understanding of the Contravariant, Divisible, and Decidable typeclasses as they appear in Haskell, and the kinds of problems to which these typeclasses have been applied.

Target Audience

The target audience is folks who get practical value from abstractions and are hungry for more

Prerequisites for Attendees

The audience is expected to have a basic understanding of Functor, Applicative and Alternative from Haskell or a similar language, and be able to read standard Haskell syntax.

schedule Submitted 1 year ago

Public Feedback

comment Suggest improvements to the Speaker

  • Liked Manuel Chakravarty
    keyboard_arrow_down

    Manuel Chakravarty - Welcome to FP Introductory Workshop

    480 Mins
    Introductory Workshop
    Beginner

    Functional programming has become inevitable. New programming languages draw inspiration from the functional paradigm; old programming languages retrofit support for functional programming; and development teams change their coding style to adopt the best functional programming idioms. We are clearly experiencing a paradigm shift in our industry.

    Due to its academic roots, functional programming sometimes seems unapproachable, with unfamiliar jargon, obscure concepts, and bewildering theories. It doesn’t have to be like that.

    In this one-day series of lectures and hands-on workshops, we will translate the jargon, demystify the concepts, and put the theories into practice. There is nothing inherently difficult about functional programming. In fact, its main aim is to simplify programming and to make it more widely accessible. Functional programming is about being able to understand one function without the million lines of code it is a part of. It is about code reuse. It is about modularity and keeping code easy to change and refactor. These are all goals of good program design that every developer appreciates. Based on this common ground, we will explore functional programming together and see how it can help us to achieve these design goals. In fact, by learning the fundamentals of functional programming in Haskell, we can improve program design in mainstream languages, such as Javascript and C++, and even more so, in hybrid languages, such as Scala and Swift.

    Throughout the day, we will explain the most commonly used functional programming terminology. You will learn the fundamentals of Haskell, one of the most popular functional programming languages. In the process, we will look at a lot of concrete code to understand what functional programming is all about and how to use it in your own programs. In the workshops, you will have plenty of opportunity to write code yourself, experiment, and ask questions. It’ll be fun!

    Bring your laptop and your curiosity and by the end of the day, functional programming will be another tool in your toolbox, and you will be ready to enjoy the main YOW! Lambda Jam conference.

  • Liked Amy Wong
    keyboard_arrow_down

    Amy Wong - Introduction to recursion scheme

    Amy Wong
    Amy Wong
    Software engineer
    BRICKX
    schedule 1 year ago
    Sold Out!
    30 Mins
    Talk
    Intermediate

    Recursion is used extensively in functional programming. It is indeed iteration through nested data structures. Therefore patterns are identified to generalise the nested structure iteration. Using these patterns can factor out the recursion mechanism from application specific logic for code simplicity. That's the purpose of recursion scheme.

    This talk points out different recursion problems are having similar patterns. The recursion scheme is introduced to support these patterns. It will discuss how the common iteration mechanics is separated from the application specific logic by using the patterns, aka morphisms, provided by the recursion scheme. The fundamental pattern, catamorphism, and a few other commonly-used morphisms will be illustrated with some recursion examples.

    The aim of the talk is to give audience an idea of how recursion can be implemented in simple and elegant ways by applying the various morphisms provided by the recursion schemes.

  • Liked Xavier Ho
    keyboard_arrow_down

    Xavier Ho - Let's Make Functional Generative Art

    Xavier Ho
    Xavier Ho
    Software Crafter
    Data61
    schedule 1 year ago
    Sold Out!
    30 Mins
    Talk
    Intermediate

    Programming generative art, also known as creative coding, is often a trial-and-error process, combining creativity and logic to present something aesthetic. However, creative coding has a high barrier to entry due to the breadth of knowledge and coding skills required. This talk aims to lower that barrier for you to take home and practice creative coding.

    In this talk, you will learn about algorithms and techniques for generative art using a pure functional paradigm. First, shepherding random numbers to simulate the universality of nature patterns that appear in plants, rocks, sand, and smoke. Next, using L-system formal grammar to create a seemingly infinite, but well-structured, series of graphics. Lastly, interfacing with printers to bring your drawing to life.

    For you to get the most out of this talk, you should be comfortable working in a functional language of your choice. Although I will introduce some theory, background in mathematics and formal theory is not required to attend.

  • Liked Andrew McMiddlin
    keyboard_arrow_down

    Andrew McMiddlin - Property-based State Machine Testing

    30 Mins
    Talk
    Intermediate

    Automated testing is key to ensuring the ongoing health and well being of any software project, giving developers and users confidence that their software works as intended. Property based testing is a significant step forward compared to traditional unit tests, exercising code with randomly generated inputs to ensure that key properties hold. However, both of these techniques tend to be used at the level of individual functions. Many important properties of an application only appear at a higher level, and depend on the state of the application under test. The Haskell library hedgehog, a relative newcomer to the property based testing world, includes facilities for property-based state machine testing, giving developers a foundation on which to build these more complicated tests.

    In this talk, Andrew will give you an introduction to state machine property testing using hedgehog.He'll start with a quick refresher on property based testing, followed by a brief introduction to state machines and the sorts of applications they can be used to model. From there, he'll take you on a guided tour of hedgehog's state machine testing facilities. Finally, Andrew will present a series of examples to show off what you can do and hopefully give you enough ideas to start applying this tool to your own projects. The application being tested will be a servant web application, and examples will include testing fundamentals such as content creation and deletion, uniqueness constraints, and authentication.

    An intermediate knowledge of Haskell and familiarity with property based testing will be beneficial,but not essential. The slides and demo application will be available after the talk for people to study in detail.

  • Liked Mark Hibberd
    keyboard_arrow_down

    Mark Hibberd - Hanging on in Quiet Desperation: Time & Programming

    Mark Hibberd
    Mark Hibberd
    Head of Technology
    Kinesis
    schedule 1 year ago
    Sold Out!
    30 Mins
    Talk
    Intermediate

    Time has a profound impact on the complexity of the systems we build.

    A significant amount of this software complexity comes from either an
    inability to recall previous states or the inability to understand
    how a state was arrived at.

    From the foundations of AI, LISP and functional programming [1], to
    causality in distributed systems [2], to the more grungy practices of
    immutable infrastructure, or the unreasonable effectiveness of
    fact-based approaches to large scale data systems; the ability to
    adequately cope with time, and the change and conflict it inevitable
    creates, is a common thread to being able to build and reason about
    these systems.

    This talk looks at the impact of time on system design. We will walk
    through examples of large-scale systems and their battles with
    complexity. At the end of the talk, the audience should start to see
    the common spectre of time and have an appreciation of how
    understanding time is fundamental to maintaining clarity, correctness
    and reliability in systems.

    [1] Situations, Actions, and Causal Laws
    John McCarthy
    http://www.dtic.mil/dtic/tr/fulltext/u2/785031.pdf
    [2] Times, Clocks and the Ordering of Events in a Distributed System
    Leslie Lamport
    https://amturing.acm.org/p558-lamport.pdf

  • Liked Sam Ritchie
    keyboard_arrow_down

    Sam Ritchie - Implementing the Elm Architecture for iOS in Swift

    Sam Ritchie
    Sam Ritchie
    Chief Codesplicer
    Codesplice
    schedule 1 year ago
    Sold Out!
    30 Mins
    Talk
    Intermediate

    Elm, for a long time the flag bearer of Functional Reactive Programming on the web, recently switched to a simpler pure functional architecture, citing 'ease of use'. Those of us that have worked on complex reactive mobile apps can sympathise with this - using FRP injudiciously in your app can make the code difficult to understand and near impossible for new developers to pick up.

    So are we able to apply some of the techniques and patterns from Elm into iOS apps? It turns out the Elm architecture is a really great fit for Swift & iOS - it allows us to:

    • reserve our FRP hammer for the use cases that make the most sense
    • separate out a simple, consistent, and highly testable pure functional core, and
    • provide a flexible, yet easily understood & applied set of architectural building blocks.

    This session will cover the basics of implementing and using an Elm-style architecture in a Swift app, will include lessons learnt in a real-world implementation, pitfalls/benefits, and implementation considerations should you adopt the approach in your own app.

  • Liked Jo Cranford
    keyboard_arrow_down

    Jo Cranford - Drinking the Elixir

    30 Mins
    Talk
    Beginner

    Elixir is a functional language that's growing in popularity as a stepping stone into functional programming, thanks to its strengths in areas like concurrency, fault tolerance and scalability combined with a syntax that is simple to learn, and feels very familiar for those with experience of languages like Ruby or Python. However, it is a very different beast – a functional style, new tools, and Erlang and Elixir libraries to learn, and while many people are experimenting with it, it's not quite mainstream for apps in the wild yet.

    Our Elixir journey began around two years ago, when we started breaking out parts of our monolith and rebuilding it as microservices in Elixir. It was a chance to introduce boundaries, and consider performance from the ground up for an application for which performance was starting to become a concern as the customer base was growing. This talk will tell the story of a journey into a new, functional language and discuss how we approached decisions around where to start breaking the monolith apart.

    The talk will also cover some of the lessons we learned, including:
    - How Elixir’s functional style made us think differently about writing code in other languages
    - Moving from ExUnit to Espec and back again
    - Tools such as umbrella apps that helped us to work with a group of Elixir microservices
    - How we planned for performance from the beginning, and Elixir features that helped us with this such as GenServer
    - Deployment strategies for the Elixir applications
    - How the migration impacted different team members including front end developers

  • Liked Brian McKenna
    keyboard_arrow_down

    Brian McKenna - Teaching functional programming at Atlassian

    Brian McKenna
    Brian McKenna
    Functional Programmer
    Atlassian
    schedule 1 year ago
    Sold Out!
    30 Mins
    Talk
    Beginner

    My team works on Atlassian Marketplace, a project which started 10 years ago and uses Scala, Haskell and Nix. The Marketplace code uses concepts such as applicative functors, monad transformers and lenses. People joining the team mostly learned concepts as they needed to, through experimentation and asking questions.

    Last year we started hosting functional programming classes for more fundamental and broad understanding. We've been using the Data61 (formerly NICTA) Functional Programming Course with a lot of success. This talk will describe how we teach FP, what we've learned about teaching FP and the challenges we face.

  • Liked Fraser Tweedale
    keyboard_arrow_down

    Fraser Tweedale - Taming the C monster: Haskell FFI techniques

    Fraser Tweedale
    Fraser Tweedale
    Software Engineer
    Red Hat
    schedule 1 year ago
    Sold Out!
    30 Mins
    Talk
    Intermediate

    Haskell has a powerful foreign function interface (FFI) for interfacing with C libraries. Haskell is a great language for building libraries and tools, but interoperability requirements or time constraints can make the FFI a compelling option.

    Binding to a non-trivial C library presents several challenges including C idioms, memory management, error handling and more. This presentation will address a selection of these concerns, using hs-notmuch, a binding to the notmuch mail indexer, as a case study. We will discuss:

    • FFI basics and tools to assist binding authors
    • working with "double pointer"-style constructors
    • working with iterators; how to do lazy iteration
    • how to use Haskell's garbage collector to manage lifecycles of external objects, and "gotchas" encountered
    • using types to enforce correct use of unsafe APIs
    • performance considerations (including profiling results)

    The presentation will conclude with a mention of some important FFI concepts that were not covered (e.g. callbacks) and a look at how hs-notmuch is being used in the Real World.

    Developers familiar with C will get the most out of this talk (because there will be limited time to explain C idioms, memory management, etc). To varying degrees, most of the concepts and techniques discussed will apply to other languages' FFIs.

  • Liked George Wilson
    keyboard_arrow_down

    George Wilson - Comma Police: Lessons From the Design and Implementation of a CSV Library

    30 Mins
    Talk
    Intermediate

    From CSV, to JSON, to YAML, DER, and the dreaded XML, many programmers are dealing with data formats all day. In statically-typed languages like Haskell, we can get a great benefit by imposing a rigid structure of types on the data we consume from these formats. We call this process of imposing structure on data "decoding".

    Decoding libraries are available in many languages and for many formats. This talk explores the design decisions behind a new Haskell library, sv, which decodes CSV and similar formats such as PSV. sv addresses what we perceived as problems with other libraries. We will discuss the benefits and drawbacks of the interesting or noteworthy choices made in sv's philosophy and design, and make recommendations as to how these lessons could be applied to other formats.

  • Liked Ivan Lazar Miljenovic
    keyboard_arrow_down

    Ivan Lazar Miljenovic - I Streamed a Stream

    90 Mins
    Code Jam
    Intermediate

    Many people are familiar with Haskell stream processing libraries such as pipes or conduit. However, they can be a bit daunting to newcomers: they contain both input and output types in the same representation and they require all these new operators rather than using well-known function composition.

    The streaming library by Michael Thompson takes a simpler approach to represent how to stream data through your code. It offers a more familiar API to anyone used to using lists, whilst still offering a lot of power and flexibility. It also compares very well in terms of performance: conduit's fusion framework tends to use a similar structure!

    In this workshop we will have an overview of the streaming ecosystem and how it compares to pipes and conduit, and start using it to efficiently process large amounts of data.

    If attending this workshop, you may wish to download the exercises (and fetch any dependencies) beforehand.

  • Liked Fraser Tweedale
    keyboard_arrow_down

    Fraser Tweedale - attoparsec acrobatics

    Fraser Tweedale
    Fraser Tweedale
    Software Engineer
    Red Hat
    schedule 1 year ago
    Sold Out!
    30 Mins
    Talk
    Intermediate

    attoparsec is a high performance parser combinator library for Haskell. It is designed for ByteString parsing and sacrifices helpful error messages and flexibility in the name of speed. Or so the story goes. It turns out that there is more flexibility in attoparsec than meets the eye.

    This presentation will familiarise attendees with attoparsec's internals and the design goals that led to the current implementation. This will provide an understanding of how attoparsec achieves its high performance, how to get the most out of it and constructions to avoid.

    Following this, we will discuss two parser use cases:

    • reporting the position at which parsing failed (often held to be impossible with attoparsec)
    • chunking an input by a fixed delimiter string (the naïve implementation is inefficient)

    We will see how attoparsec's internals can be exploited to satisfy these use cases. Along the way, some other interesting parsers will be discovered. We will also discuss how the efficient chunking combinator could be constructed in other libraries. The performance impact of the efficient chunking combinator for MIME parsing in the purebred-email library will be demonstrated.

    This presentation assumes familiarity with Haskell syntax and parser combinator library usage. Concepts are applicable in other libraries and languages.

  • Liked Marcin Moskała
    keyboard_arrow_down

    Marcin Moskała - Functional programming in Kotlin

    Marcin Moskała
    Marcin Moskała
    Sr. Android Developer
    Gamekit
    schedule 1 year ago
    Sold Out!
    30 Mins
    Talk
    Intermediate

    Functional programming is becoming more and more popular each day. It is used to make parts of applications highly testable and reusable. Kotin introduces functional programming mechanisms, but for most programmers, it is still difficult to use them in a smart way. This lecture will show how to use functional programming in Kotlin to make clean and reliable code.