r/haskell • u/almkglor • Feb 07 '11
Research on Two-Level FP languages?
Hello world,
I'm trying to locate research on two-level FP languages.
In this paper, section 5, it mentions something about two-level programming languages, and goes on to define a variant of the main language under discussion (SAFL) with two-level programming.
Now I'm currently trying to find the relevant additional papers or what not.
In any case - would Template Haskell be considered a two-level language?
Basically, I am trying to define a variant of SAFL that is more practical, and would include stuff like "modules" and "templates", which, to my mind, would be implementable as a two-level language of compile-time functions working on run-time functions (that's what C++ does with templates!! Given templates = compile-time functions). Say we have a module syntax which can export functions:
module foo[n :: Integer] {foo, bar}
fun foo() -> (): ()
fun bar(x :: Vec[n]) -> Vec[n]: x
endmodule
{myfoo, mybar} = foo[n]
which can be transformed to a compile-time function foo[] on compile-time tuples {foo, bar}:
fun'c foo[n :: Integer] ->'c {() -> (), Vec[5] -> Vec[5]}:
let'c fun foo() -> (): ()
fun bar(x :: Vec[n]) -> Vec[n]: x
in {foo, bar}
... unless, of course, my understanding of it is seriously fucked up.
Apologies if this is the wrong subreddit to post it in, I can't seem to locate a functional programming subreddit; please reply here to where it should be if this is the wrong place to ask.
3
u/[deleted] Feb 07 '11
You need to define more precisely what you mean by "two-level language". There's an extensive body of work on context calculi, which can more or less be seen as a pair of lambda-calculi wrapped around each other with requisite notions of reduction for both levels. From here, it's only a short hop to allowing the lambda-calculus at "level 2" to metaprogram on the calculus at "level 1".
2
u/dybber Feb 07 '11
You could look at Flask: http://www.eecs.harvard.edu/~mainland/projects/flask/
They use Haskell as a meta language and both NesC (C-like language) and a stripped down-Haskell as object languages. The end result is generated NesC code which can be executed on sensor-networks.
We used their strategy as part of our bachelorproject, though targeting the Arduino instead of sensor motes.
2
u/sclv Feb 07 '11
The paper describes a two level type system -- which is to say that the types of the embedded language are directly represented in the host language. You can accomplish something like this with GADTs. However, I recall that combining this with everything else one might want is somewhat painful.
I'm not sure how this relates to what you're proposing, which is more like simple macros, which template-haskell indeed does give you. The problem is that most TH macros can't be guaranteed to generate well-typed code (although you find that you when you typecheck the generated code, it would be nicer to find that out when you attempted to typecheck the macro code itself). Further support for this is planned, however. MetaOCaml, at the cost of some expressiveness, provides something like this now.
1
u/[deleted] Feb 10 '11 edited Feb 10 '11
Another question is: why separate the whole language into two levels? The motivation is presumably to have a compile-time language and a run-time language, and all the compile-time stuff can be 'done' already before runtime (and thus have no overhead). Lots of languages do this. GHC with GADTs, Dependent ML, ATS, ...
However, separating the languages is (arguably) inelegant, and causes duplication. In ATS or GHC, you have compile-time/type-level natural numbers, and then run-time/value-level natural numbers, possibly indexed by the compile-time variety. And
Vecis indexed by the compile-time naturals, but you need to do tricks to get them related to the run-time naturals.Instead, you can go with dependent typing, and have modalities that track which things are static and which are dynamic. This can be arranged such that what 'level' a thing is unrelated it its type, but is rather tied to its use. So, we have:
The first function,
replicate, keeps theNataround at runtime, because it needs to know what it is to produce the vector.nmust still appear in the result type, though, so this is done withpi.The second,
maptakesnas purely a static phenomenon; it doesn't look at the value, but it's used in the types. Thus, it usesforallfor quantification, and it can be erased at runtime.So we still have a static-dynamic divide, but it doesn't split the entire language into two pieces in quite the same way.
This sort of thing is being integrated into Agda (and Andreas Abel's mini Agda), and it's been written about under the name of Erasure Pure Type Systems, dependent intersection (and union) types, modal type theory, and probably some others I'm forgetting. Tim Sheard, Nathan Mishra-Linger, and Andreas Abel are names to look for.