fork download
  1. {-# LANGUAGE TypeFamilies #-}
  2.  
  3. class ProjResult a where
  4. projection :: Int -> a
  5. constant :: Int -> a
  6.  
  7. instance ProjResult Int where
  8. projection _ = error "index too large"
  9. constant x = x
  10.  
  11. instance (ProjResult a, arg ~ Int) => ProjResult (arg -> a) where
  12. projection 0 x = constant x
  13. projection n _ = projection (n-1)
  14. constant j _ = constant j
  15.  
  16. p :: Int -> Int -> Int -> Int -> Int
  17. p = projection
  18.  
  19. main = do
  20. print $ p 2 1 2 3
  21. print (projection 2 1 2 3 :: Int) -- 型注釈はこれだけでいい
  22.  
Success #stdin #stdout 0s 6260KB
stdin
Standard input is empty
stdout
3
3