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)
);

例如,考虑两个电阻连接在一个回路中的(有点无意义的)电路:

        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.GraphData.Graph.Inductive.

于 2012-10-04T20:50:03.453 回答