style pattern matching with type checking
I am working on a project to recreate some of the features of Haskell in Clojure. I have succeeded in implementing Hindley-Milner type inference, and now I am trying to introduce pattern-matching syntax via a macro. My goal is to have the macro first emit valid Clojure code and then type-check that code, but I am finding this hard to do. Since I'm brand new to Haskell and static typing in general, I'm not sure if there's a way to do it that I don't see, or whether it's just not possible.
I'm attempting to follow Luc Maranget's algorithm, at least the naive version for now. My understanding is that you take a vector of values (or "occurrences" at compile time) and treat it as a stack. After grouping the rows of patterns by the constructors of the first column, you find the group of patterns corresponding to the constructor of the first value in the vector. If the vector is (v1 ... vn)
, and v1
is c(a1 ... ak)
, where c
is the constructor and a1 ... ak
are the arguments to the constructor, then you drop the constructor c
and concatenate the arguments with the vector, creating the new vector (a1 ... ak v2 ... vn)
, and run the algorithm recursively.
My question is, how do you represent such a vector or stack in a type-checkable way? Again, my goal is for the Clojure macro to emit code that is both ready to handle runtime values but also ready to be type-checked at compile time. I could introduce an intermediate step, wherein the first macro emits code that is type-checkable but is also itself a macro, and then have the second macro emit the code that will actually run. But even if that works, it's less satisfying because it suggests that I wouldn't be able to accomplish a task like this in Haskell itself. Am I missing something?
-- EDIT --
I now see that my confusion arose from not having encountered a situation in my short experience with static typing in which I had to deal with a heterogeneous collection. My question could have been formulated more clearly, but since I'm mapping out the problem space as I go along, I am not always sure exactly what an obstacle is or how it's related to everything else I'm dealing with. Thanks to the comments below, I believe that to surmount this particular challenge I just need to have a better understanding of the options available for dealing with heterogeneous collections in Haskell. This article seems to be a good starting point.
链接地址: http://www.djcxy.com/p/80894.html上一篇: 在Ruby中包含和扩展有什么区别?
下一篇: 样式模式匹配与类型检查