4

I am trying to follow up on the paper "Lightweight higher-kinded polymorphism" (https://ocamllabs.github.io/higher/lightweight-higher-kinded-polymorphism.pdf) and I am stuck at transforming this ML code into F#

type (_,_) arrow =
    Fn_plus : ((int ∗ int), int) arrow
    | Fn_plus_cons : int → ((int ∗ int list), int list) arrow

and

let apply : type a b. (a, b) arrow ∗ a → b =
    fun (appl, v) → match appl with
    | Fn_plus → let (x, y) = v in x + y
    | Fn_plus_cons n → let (x, l’) = v in x + n :: l’ 

Specifically the type definition is feeling like a big magic wall.

Guy Coder
  • 24,501
  • 8
  • 71
  • 136
robkuz
  • 9,488
  • 5
  • 29
  • 50
  • 1
    F# doesn't support higher-kinded polymorphism. Sorry, there is no way to do this without runtime hacks. – Fyodor Soikin Dec 06 '16 at 13:42
  • 2
    @FyodorSoikin - The paper is about encoding higher-kinded polymorphism in languages which don't support it directly. – Lee Dec 06 '16 at 13:54
  • 2
    yeah, I know. But as far as I know neither does Ocaml nor ML. And the paper above explains a de-functionalization approach to create "lightweight higher kinded" types. I think the 2 code snippets dont have HKTs – robkuz Dec 06 '16 at 13:55
  • Of Interest: [MLton Elaborate](http://mlton.org/Elaborate) - Don't know if it will help for this specific question but adding a link for others on the same trail that pass by here. – Guy Coder Dec 06 '16 at 14:03
  • Of Interest: [SML .NET](https://www.cl.cam.ac.uk/research/tsg/SMLNET/) – Guy Coder Dec 06 '16 at 14:05
  • Have a look at Palladin's [Higher](https://github.com/palladin/Higher) library. – Keith Pinson Feb 08 '19 at 19:02

1 Answers1

5

This example uses GADTs (sort of like discriminated unions where the individual union cases can constrain the type's parameters in different ways), which are not available in F#. Thankfully, this is just part of an introduction to the idea of (value) defunctionalization, so I think it's not actually critical to the part of the paper you care about.

As an aside, one way to encode GADTs in a language with higher-kinded types is shown here, so you can actually encode the GADTs using the "Lightweight higher-kinded polymorphism" approach itself.

Another, simpler, approach is demonstrated in the "Simplistic GADTs" section here, which is mostly straightforward to translate to F#. However, note the caveat mentioned there that the Leibniz principle isn't quite fully supported (and look at the other sections of the page to see a superior extension of approach that again requires higher kinds).

kvb
  • 54,864
  • 2
  • 91
  • 133