[Haskell-cafe] dependent types, singleton types....

Richard Eisenberg eir at cis.upenn.edu
Wed Apr 29 11:43:45 UTC 2015


Hello Mark,

Your suspicion that your singleton tree type is wrong is well-founded.

The problem is that, in my opinion, that exercise is mentioned too early in the tutorial. To properly implement a singleton type for a parameterized type, like a binary tree, you will need `data family Sing (a :: k)`, as explained just a little bit further down in the post. You'll need to rewrite your definition for singleton numbers and booleans to work with `Sing` as well.

Your code except the definition for SBranch is all correct. The problem with your definition is that you don't get the right information when pattern-matching. For example, say you have x with type `SBTree a`. If you successfully pattern match against `SBranch SZ SLeaf SLeaf`, you would want to learn `a ~ Branch Z Leaf Leaf`. But that's not what you'll get in your implementation: you'll get a type error saying that we don't know that `a0` is an `SNat`, where `a ~ Branch a0 Leaf Leaf`, or something like that. The type-level information is simply encoded in the wrong place for this to work out.

Write back and I'll give you the full answer if this isn't enough to get you moving in the right direction!

Richard

On Apr 28, 2015, at 10:45 AM, "Nicholls, Mark" <nicholls.mark at vimn.com> wrote:

> Can someone check my answer (no I’m not doing an assessment…I’m actually learning stuff out of interest!)
>  
> working through
>  
> https://www.fpcomplete.com/user/konn/prove-your-haskell-for-great-safety/dependent-types-in-haskell
>  
> still there is a section about singleton types and the exercise is
>  
> “Exercise: Define the binary tree type and implement its singleton type.”
>  
> Ok, I think I’m probably wrong….a binary tree is something like…
>  
> > data BTree a = Leaf | Branch a (BTree a) (BTree a)
>  
> With DataKind
>  
> My logic goes…
> Leaf is an uninhabited type, so I need a value isomorphic to it….
>  
> Easy?
>  
> > data SBTree a where
> >   SLeaf :: SBTree Leaf
>  
> Things like
> Branch Integer Leaf  (Branch String Leaf Leaf)
> Are uninhabited…so I need to add
>  
> >   SBranch :: (a :: *) -> (SBTree (b :: BTree *)) -> (SBTree (c :: BTree *)) -> SBTree (Branch a b c)
>  
> ?
>  
> It compiles…but….is it actually correct?
> Things like
>  
> > y = SBranch (SS (SS SZ)) SLeaf SLeaf
> > z = SBranch (SS (SS SZ)) (SBranch SZ SLeaf SLeaf) SLeaf
>  
> Seem to make sense ish.
>  
> From: Nicholls, Mark 
> Sent: 28 April 2015 9:33 AM
> To: Nicholls, Mark
> Subject: sds
>  
> Hello,
>  
> working through
>  
> https://www.fpcomplete.com/user/konn/prove-your-haskell-for-great-safety/dependent-types-in-haskell
>  
> but a bit stuck...with an error...
>  
> > {-# LANGUAGE DataKinds, TypeFamilies, TypeOperators, UndecidableInstances, GADTs, StandaloneDeriving #-}
>  
> > data Nat = Z | S Nat
>  
> > data Vector a n where
> >   Nil  :: Vector a Z
> >   (:-) :: a -> Vector a n -> Vector a (S n)
> > infixr 5 :-
>  
> I assume init...is a bit like tail but take n - 1 elements from the front....but...
>  
> > init' :: Vector a ('S n) -> Vector a n
> > init' (x :- Nil) = Nil
> > init' (x :- xs@(_ :- _)) = x :- (init' xs)
>  
> > zipWithSame :: (a -> b -> c) -> Vector a n -> Vector b n -> Vector c n
> > zipWithSame f Nil Nil = Nil
> > zipWithSame f (x :- xs) (y :- xs@(_ :- _)) = Nil
>  
> Mark Nicholls | Senior Technical Director, Programmes & Development - Viacom International Media Networks
> A: 17-29 Hawley Crescent London NW1 8TT | e: Nicholls.Mark at vimn.com T: +44 (0)203 580 2223
>  
> <image001.png>
>  
> 
> 
> CONFIDENTIALITY NOTICE
> 
> This e-mail (and any attached files) is confidential and protected by copyright (and other intellectual property rights). If you are not the intended recipient please e-mail the sender and then delete the email and any attached files immediately. Any further use or dissemination is prohibited.
> 
> While MTV Networks Europe has taken steps to ensure that this email and any attachments are virus free, it is your responsibility to ensure that this message and any attachments are virus free and do not affect your systems / data.
> 
> Communicating by email is not 100% secure and carries risks such as delay, data corruption, non-delivery, wrongful interception and unauthorised amendment. If you communicate with us by e-mail, you acknowledge and assume these risks, and you agree to take appropriate measures to minimise these risks when e-mailing us.
> 
> MTV Networks International, MTV Networks UK & Ireland, Greenhouse, Nickelodeon Viacom Consumer Products, VBSi, Viacom Brand Solutions International, Be Viacom, Viacom International Media Networks and VIMN and Comedy Central are all trading names of MTV Networks Europe.  MTV Networks Europe is a partnership between MTV Networks Europe Inc. and Viacom Networks Europe Inc.  Address for service in Great Britain is 17-29 Hawley Crescent, London, NW1 8TT.
> 
> _______________________________________________
> Haskell-Cafe mailing list
> Haskell-Cafe at haskell.org
> http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.haskell.org/pipermail/haskell-cafe/attachments/20150429/5cfd0d5d/attachment.html>


More information about the Haskell-Cafe mailing list