考虑 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 只是为了让我可以精确地定义我试图表示的结构。