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

Nicholls, Mark nicholls.mark at vimn.com
Tue Apr 28 14:45:54 UTC 2015


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<mailto:mik at vimn.com> T: +44 (0)203 580 2223

[Description: cid:image001.png at 01CD488D.9204D030]

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.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.haskell.org/pipermail/haskell-cafe/attachments/20150428/d8a9b62c/attachment.html>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: image001.png
Type: image/png
Size: 22096 bytes
Desc: image001.png
URL: <http://mail.haskell.org/pipermail/haskell-cafe/attachments/20150428/d8a9b62c/attachment.png>


More information about the Haskell-Cafe mailing list