Szukam algorytmu wnioskowania typu dla języka, który rozwijam, ale nie mogłem znaleźć takiego, który odpowiada moim potrzebom, ponieważ zwykle są to:
- à la Haskell, z polimorfizmem, ale bez przeładowania ad hoc
- à la C ++ (auto), w którym występuje przeciążenie ad-hoc, ale funkcje są monomorficzne
W szczególności mój system typów (upraszcza) (używam składni Haskellisha, ale jest to zależne od języka):
data Type = Int | Double | Matrix Type | Function Type Type
I mam operatora *, który ma dość przeciążenia:
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
Itp...
I chcę wywnioskować możliwe typy dla
(2*(x => 2*x))*6
(2*(x => 2*x))*{{1,2},{3,4}}
Pierwszy to Int
drugi Matrix Int
.
Przykład (to nie działa):
{-# 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