我正在寻找一种针对我正在开发的语言的类型推断算法,但找不到适合我需求的语言,因为它们通常是:
- Haskellà具有多态性,但没有临时重载
- àla C ++(自动),其中有特定的重载,但函数是单态的
特别是我的类型系统是(简化)(我使用的是Haskellish语法,但这与语言无关):
data Type = Int | Double | Matrix Type | Function Type Type
而且我有一个运算符*,其中有很多重载:
Int -> Int -> Int
(Function Int Int) -> Int -> Int
Int -> (Function Int Int) -> (Function Int Int)
(Function Int Int) -> (Function Int Int) -> (Function Int Int)
Int -> Matrix Int -> Matrix Int
Matrix Int -> Matrix Int -> Matrix Int
(Function (Matrix Int) (Matrix Int)) -> Matrix Int -> Matrix Int
等等...
我想推断出可能的类型
(2*(x => 2*x))*6
(2*(x => 2*x))*{{1,2},{3,4}}
第一个是Int
,第二个Matrix Int
。
示例(不起作用):
{-# LANGUAGE OverlappingInstances, MultiParamTypeClasses,
FunctionalDependencies, FlexibleContexts,
FlexibleInstances, UndecidableInstances #-}
import qualified Prelude
import Prelude hiding ((+), (*))
import qualified Prelude
newtype WInt = WInt { unwrap :: Int }
liftW f a b = WInt $ f (unwrap a) (unwrap b)
class Times a b c | a b -> c where
(*) :: a -> b -> c
instance Times WInt WInt WInt where
(*) = liftW (Prelude.*)
instance (Times a b c) => Times a (r -> b) (r -> c) where
x * g = \v -> x * g v
instance Times (a -> b) a b where
f * y = f y
two = WInt 2
six = WInt 6
test :: WInt
test = (two*(\x -> two*x))*six
main = undefined
3
这似乎不符合CS Theory Stack Exchange的标准,但看起来确实在寻找更数学或理论上的答案。我认为计算机科学可能适用于此。由于您请求采取行动以获得更好的答案,因此,我会将其发送到可能会受到广泛欢迎的地方。
—
Thomas Owens 2014年