Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
131 views
in Technique[技术] by (71.8m points)

Are these Haskell sub-classes redundant?

The code in Listing 1 represents my attempt to implement the following requirement:

It can be shown that a passenger on a boat in a boathouse can be said to be indirectly in the boathouse.

The rather unusual formalisation of the requirement is based on code used in a previous question. I am not seeking a drastic alternative coding nor to included Haskell extensions. For research purposes, I need to stay as close to the original coding as possible.

It seems to me that the sub-classes are redundant because functions can only be defined in instances of the class where they were originally declared, not in any sub-class of that original class. Is this correct?

Listing 1

{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE MultiParamTypeClasses #-}
{-#  OPTIONS_GHC  -Wno-missing-methods #-}
-- The suffix D=data type,Dc=constructor
-- The data types were chosen to match the type variables in the type classes
data PeopleD = PeopleDc [Char] deriving (Show,Eq)
data BoatD a  = BoatDc a deriving (Show,Eq)
data HouseBoatD a  = HouseBoatDc a  deriving (Show,Eq)
data BoatHouseD a  = BoatHouseDc a deriving (Show,Eq)
                
                 
class ContainersC a b where -- suffix C= class
 insert :: b -> a b -> a b
 remove :: b -> a b -> a b
 whatsIn :: (a b) -> [b]
                
 class SurfacesC a b where  
  put ::  b -> a b -> a b
  takeOff :: b -> a b -> a b
  whatsOn :: (a b) -> [b] 
                               
                
 class PeopleC p
 -- subclasses
 class ContainersC h o => HousesC h o
 class (SurfacesC v o) => BoatsC v o
 class (HousesC h (v p), BoatsC v p) => BoatHousesC h v p
 class (BoatsC v p, HousesC v p, PeopleC p) => HouseBoatsC v p 
                             
               
 instance ContainersC BoatD PeopleD where
   whatsIn (BoatDc (PeopleDc x)) = [PeopleDc x]
                
 instance ContainersC BoatHouseD (BoatD PeopleD) where
  whatsIn (BoatHouseDc (BoatDc (PeopleDc x))) = [(BoatDc(PeopleDc x))]
              
 instance SurfacesC BoatD PeopleD where
  whatsOn (BoatDc (PeopleDc x)) = [PeopleDc x]
         
 test =  (head (whatsOn(head (whatsIn (BoatHouseDc (BoatDc (PeopleDc "joe"))))))) == 
         (head (whatsOn(BoatDc (PeopleDc "joe"))))
question from:https://stackoverflow.com/questions/65952850/are-these-haskell-sub-classes-redundant

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...