Why not be dependently typed?

前端 未结 4 1282
情话喂你
情话喂你 2020-12-02 03:50

I have seen several sources echo the opinion that \"Haskell is gradually becoming a dependently-typed language\". The implication seems to be that with more and more languag

4条回答
  •  刺人心
    刺人心 (楼主)
    2020-12-02 04:04

    John that's another common misconception about dependent types: that they don't work when data is only available at run-time. Here's how you can do the getLine example:

    data Some :: (k -> *) -> * where
      Like :: p x -> Some p
    
    fromInt :: Int -> Some Natty
    fromInt 0 = Like Zy
    fromInt n = case fromInt (n - 1) of
      Like n -> Like (Sy n)
    
    withZeroes :: (forall n. Vec n Int -> IO a) -> IO a
    withZeroes k = do
      Like n <- fmap (fromInt . read) getLine
      k (vReplicate n 0)
    
    *Main> withZeroes print
    5
    VCons 0 (VCons 0 (VCons 0 (VCons 0 (VCons 0 VNil))))
    

    Edit: Hm, that was supposed to be a comment to pigworker's answer. I clearly fail at SO.

提交回复
热议问题