2

MySQL の電気回路の次の (簡略化された) モデルを考えてみましょう。

create table circuit (
  id int not null auto_increment primary key,
  name varchar(30) not null
);
create table device (
  id int not null auto_increment primary key,
  circuit_id int not null references circuit (id),
  chip varchar(30) not null,
  primary key (id)
);
create table pin (
  id int not null auto_increment primary key,
  device_id int not null references device (id),
  name varchar(10) not null unique,
  unique (device_id, name)
);
create table wire (
  circuit_id int not null references circuit (id),
  pin1_id int not null unique references pin (id),
  pin2_id int not null unique references pin (id),
  primary key (pin1_id, pin2_id)
);

例として、ループに接続された 2 つの抵抗器の (やや無意味な) 回路を考えてみましょう。

        RES
    +---/\/\/--+
    |   A   B  |
    |          |
    |   RES    |
    ----/\/\/--+
        B   A

先ほど説明したデータベース構造を使用すると、これは次のように表すことができます。

insert into circuit (name) values ('example');
set @c = last_insert_id();

insert into device (circuit_id, chip) values (@c, 'RES');
set @r1 = last_insert_id();
insert into pin (device_id, name) values (@r1, 'A');
set @a1 = last_insert_id();
insert into pin (device_id, name) values (@r1, 'B');
set @b1 = last_insert_id();

insert into device (circuit_id, chip) values (@c, 'RES');
set @r2 = last_insert_id();
insert into pin (device_id, name) values (@r2, 'A');
set @a2 = last_insert_id();
insert into pin (device_id, name) values (@r2, 'B');
set @b2 = last_insert_id();

insert into wire (circuit_id, pin1_id, pin2_id) values
  (@c, @b1, @a2), (@c, @b2, @a1);

この種の構造を Haskell で表現するにはどうすればよいでしょうか? データベースに結び付けられることはありません。SQL は、表現しようとしている構造を正確に定義できるようにするためのものです

4

1 に答える 1

4

あなたのpinテーブルはグラフの隣接リストに似ているので、グラフライブラリを使用します-Data.GraphまたはData.Graph.Inductive.

于 2012-10-04T20:50:03.453 に答える