To put that another way, if `forall a.` introduces types (it's like the big lambda), then you can only have a type with `forall`s on the left on the outside. So:
`forall a. forall b. a + b` is a type but
`forall a. a -> forall b. b -> c` is not a type since that `forall` isn't all the way on the left in a chain like the first one.
If you're not in that fragment, and you're trying to infer types, you're gonna have a bad time unless you've got something exceedingly clever up your sleeve.
So `ForAll` should not be one of the "Type" type constructors. It should be its own type, like
# The int is the number of variables bound, then the type is the type that uses them
data Scheme = Int Type
data Type = 0 | 1 | Var Int | Product Type Type | ...Just fyi, this is called System F.
I do not remember any OCaml and I get an error when trying to run it (File "./test.ml", line 123, characters 6-7: Error: Syntax error: operator expected.) so I do not know if I can be of much help. What is the result of typ_of_expr [] (app k_intint (Int 1))? I presume that you expect it to be IntTy -> IntTy, right?
It is indeed system F I'm trying to implement :-)
My formatting for HN seems to have screwed the program up. You'll find the contents of systemf.ml in this paste: https://pastebin.com/gw20LBNR
Here is the result from my OCaml repl:
# typ_of_expr [] (app k_intint (Int 1));;
- : (typ, string) result =
Error "Type mismatch. t0_0: SymbolTy a, t0_1: Arrow, t1: IntTy"
You are correct about what `(k_intint (Int 1))` should be. I hope for `k_intint` to be `IntTy -> IntTy -> IntTy`. ty(TApp ...) = Closure (Arrow (SymbolTy a) (Arrow (SymbolTy _b) (SymbolTy a)))
ty(Int 1) = IntTy
ty(App (TApp ...) (Int 1)) = Failure
"Type mismatch. t0_0: SymbolTy a, t0_1: Arrow (SymbolTy _b) (SymbolTy a), t1: IntTy".
Here `t0_0` is a formal argument type and `t1` is being applied, so even though they are structurally different it should be inferred that `SymbolTy a` equals to `IntTy` (probably by updating the context). But your code doesn't do that.I strongly recommend to first familize yourself with Hindley-Milner type system (and possibly Algorithm W) which type inference is efficient and easy enough to understand. Even though the decidable type inference in System F is impossible, the basic techniques for (partial) type inference are not too different.
Two things I see missing from your code: A current substitution and a unification function.
This paper might also be helpful to read: http://citeseerx.ist.psu.edu/viewdoc/download?doi=10.1.1.65....
I'm reaching out for some help from you. I've been working on interpreters this whole year, taking on no more work for pay than needed for rent and food, so that I can realize my programming language dream. I'm writing a couple of interpreters every week, trying out new ideas. But there is one hurdle I've been unable to overcome for a long time.
I'm trying to write a typechecker for a simple lambda calculus with polymorphic functions. My acceptance criteria is being able to typecheck the K combinator. I've been stuck at this point for months. Have read all I can find on the internet, and have read TAPL many times.
Am honestly feeling really stupid for not having been able to complete this.
What I'm struggling with beta reducing expressions that are type lambdas (i.e. the expression related to `Forall`).
Λ means type lambda, i.e. the set of expressions indexed by type
λ means expression lambda, i.e. the set of expressions indexed by expression
E.g. Λa.λx:a.x is the polymorphic "identity" function, which in javascript looks like `x => x`
k = ΛA.ΛB.λx:A.λy:B.x
k_intint = ((k IntTy) IntTy)
What I have yet been unable to do is type_of k_intint, and this because beta reduction should return an expression, but type_of should return a type.
I'll paste the source code of my latest attempt below, the function in question is `typ_of_expression`. I've written this in OCaml, but would be very thankful for a response in any language, or pseudocode, or just explained in English.
Many thanks for taking the time to read this. For any reply or pointer in the right direction I would be greatly thankful. You know what, I'd be happy to pay €100 for any help that lets me solve this (SEPA, Bitcoin).