Haskell でデータ構造をモデル化するのに問題があります。私が動物研究施設を運営していて、ネズミを追跡したいとします。ケージと実験へのラットの割り当てを追跡したいと思います。また、ラットの重量、ケージの容積を追跡し、実験のメモを取りたいと思っています。
SQL では、次のようにします。
create table cages (id integer primary key, volume double);
create table experiments (id integer primary key, notes text)
create table rats (
weight double,
cage_id integer references cages (id),
experiment_id integer references experiments (id)
);
(これにより、異なる実験からの2匹のラットを同じケージに割り当てることができることを認識しています.これは意図されています.私は実際に動物研究施設を運営していません.)
可能でなければならない 2 つの操作: (1) ラットが与えられた場合、そのケージの容積を見つけ、(2) ラットが与えられた場合、それが属する実験のメモを取得します。
SQLでは、それらは
select cages.volume from rats
inner join cages on cages.id = rats.cage_id
where rats.id = ...; -- (1)
select experiments.notes from rats
inner join experiments on experiments.id = rats.experiment_id
where rats.id = ...; -- (2)
このデータ構造を Haskell でモデル化するにはどうすればよいでしょうか?
それを行う1つの方法は
type Weight = Double
type Volume = Double
data Rat = Rat Cage Experiment Weight
data Cage = Cage Volume
data Experiment = Experiment String
data ResearchFacility = ResearchFacility [Rat]
ratCageVolume :: Rat -> Volume
ratCageVolume (Rat (Cage volume) _ _) = volume
ratExperimentNotes :: Rat -> String
ratExperimentNotes (Rat _ (Experiment notes) _) = notes
Cage
しかし、この構造ではs とsのコピーが大量に導入されるのではないでしょうExperiment
か? それとも、それについて心配する必要はなく、オプティマイザーがそれを処理してくれることを願っていますか?