Hide keyboard shortcuts

Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122

123

124

125

126

127

128

129

130

131

132

133

134

135

136

137

138

139

140

141

142

143

144

145

146

147

148

149

150

151

152

153

154

155

156

157

158

159

160

161

162

163

164

165

166

167

168

169

170

171

172

173

174

175

176

177

178

179

180

181

182

183

184

185

186

187

188

189

190

191

192

193

194

195

196

197

198

199

200

201

202

203

204

205

206

207

208

209

210

211

212

213

214

215

216

217

218

219

220

221

222

223

224

225

226

227

228

229

230

231

232

233

234

235

236

237

238

239

240

241

242

243

244

245

246

247

248

249

250

251

252

253

254

255

256

257

258

259

260

261

262

263

264

265

266

267

268

269

270

271

272

273

274

275

276

277

278

279

280

281

282

283

284

285

286

287

288

289

290

291

292

293

294

295

296

297

298

299

300

301

302

303

304

305

306

307

308

309

310

311

312

313

314

315

316

317

318

319

320

321

322

323

324

325

326

327

328

329

330

331

332

333

334

335

336

337

338

339

340

341

342

343

344

345

346

347

348

349

350

351

352

353

354

355

356

357

358

359

360

361

362

363

364

365

366

367

368

369

370

371

372

373

374

375

376

377

378

379

380

381

382

383

384

385

386

387

388

389

390

391

392

393

394

395

396

397

398

399

400

401

402

403

404

405

406

407

408

409

410

411

412

413

414

415

416

417

418

419

420

421

422

423

424

425

426

427

428

429

430

431

432

433

434

435

436

437

438

439

440

441

442

443

444

445

446

447

448

449

450

451

452

453

454

455

456

457

458

459

460

461

462

463

464

465

466

467

468

469

470

471

472

473

474

475

476

477

478

479

480

481

482

483

484

485

486

487

488

489

490

491

492

493

494

495

496

497

498

499

500

501

502

503

504

505

506

507

508

509

510

511

512

513

514

515

516

517

518

519

520

521

522

523

524

525

526

527

528

529

530

531

532

533

534

535

536

537

538

539

540

541

542

543

544

545

546

547

548

549

550

551

552

553

554

555

556

557

558

559

560

561

562

563

564

565

566

567

568

569

570

571

572

573

574

575

576

577

578

579

580

581

582

583

584

585

586

587

588

589

590

591

592

593

594

595

596

597

598

599

600

601

602

603

604

605

606

607

608

609

610

611

612

613

614

615

616

617

618

619

620

621

622

623

624

625

626

627

628

629

630

631

632

633

634

635

636

637

638

639

640

641

642

643

644

645

646

647

648

649

650

651

652

653

654

655

656

657

658

659

660

661

662

663

664

665

666

667

668

669

670

671

672

673

674

675

676

677

678

679

680

681

682

683

684

685

686

687

688

689

690

691

692

693

694

695

696

697

698

699

700

701

702

703

704

705

706

707

708

709

710

711

712

713

714

715

716

717

718

719

720

721

722

723

724

725

726

727

728

729

730

731

732

733

734

735

736

737

738

739

740

741

742

743

744

745

746

747

748

749

750

751

752

753

754

755

756

757

758

759

760

761

762

763

764

765

766

767

768

769

770

771

772

773

774

775

776

777

778

779

780

781

782

783

784

785

786

787

788

789

790

791

792

793

794

795

796

797

798

799

800

801

802

803

804

805

806

807

808

809

810

811

812

813

814

815

816

817

818

819

820

821

822

823

824

825

826

827

828

829

830

831

832

833

834

835

836

837

838

839

840

841

842

843

844

845

846

847

848

849

850

851

852

853

854

855

856

857

858

859

860

861

862

863

864

865

866

867

868

869

870

871

872

873

874

875

876

877

878

879

880

881

882

883

884

885

886

887

888

889

890

891

892

893

894

895

896

897

898

899

900

901

902

903

904

905

906

907

908

909

910

911

912

913

914

915

916

917

918

919

920

921

922

923

924

925

926

927

928

929

930

931

932

933

934

935

936

937

938

939

940

941

942

943

944

945

946

947

948

949

950

951

952

953

954

955

956

957

958

959

960

961

962

963

964

965

966

967

968

969

970

971

972

973

974

975

976

977

978

979

980

981

982

983

984

985

986

987

988

989

990

991

992

993

994

995

996

997

998

999

1000

1001

1002

1003

1004

1005

1006

1007

1008

1009

1010

1011

1012

1013

1014

1015

1016

1017

1018

1019

1020

1021

1022

1023

1024

1025

1026

1027

1028

1029

1030

1031

1032

1033

1034

1035

1036

1037

1038

1039

1040

1041

1042

1043

1044

1045

1046

1047

1048

1049

1050

1051

1052

1053

1054

1055

1056

1057

1058

1059

1060

1061

1062

1063

1064

1065

1066

1067

1068

1069

1070

1071

1072

1073

1074

1075

1076

1077

1078

1079

1080

1081

1082

1083

1084

1085

1086

1087

1088

1089

1090

1091

1092

1093

1094

1095

1096

1097

1098

1099

1100

1101

1102

1103

1104

1105

1106

1107

1108

1109

1110

1111

1112

1113

1114

1115

1116

1117

1118

1119

1120

1121

1122

1123

1124

1125

1126

1127

1128

1129

1130

1131

1132

1133

1134

1135

1136

1137

1138

1139

1140

1141

1142

1143

1144

1145

1146

1147

1148

1149

1150

1151

1152

1153

1154

1155

1156

1157

1158

1159

1160

1161

1162

1163

1164

1165

1166

1167

1168

1169

1170

1171

1172

1173

1174

1175

1176

1177

1178

1179

1180

r""" 

Set factories 

============= 

 

 

A *set factory* `F` is a device for constructing some :class:`Parent` 

`P` that models subsets of a big set `S`. Typically, each such parent 

is constructed as the subset of `S` of all elements satisfying a 

certain collection of constraints `cons`. In such a hierarchy of 

subsets, one needs an easy and flexible control on how elements are 

constructed. For example, one may want to construct the elements of 

`P` in some subclass of the class of the elements of `S`. On other 

occasions, one also often needs `P` to be a facade parent, whose 

elements are represented as elements of `S` (see 

:class:`~sage.categories.facade_sets.FacadeSets`). 

 

The role of a set factory is twofold: 

 

- *Manage a database* of constructors for the different parents `P = F(cons)` 

depending on the various kinds of constraints `cons`. Note: currently there 

is no real support for that. We are gathering use cases before fixing the 

interface. 

 

- Ensure that the elements `e = P(...)` created by the different parents 

follows a consistent policy concerning their *class and parent*. 

 

.. RUBRIC:: Basic usage: constructing parents through a factory 

 

The file :mod:`sage.structure.set_factories_example` shows an example of a 

:class:`SetFactory` together with typical implementation. Note that the 

written code is intentionally kept minimal, many things and in particular 

several iterators could be written in a more efficient way. 

 

Consider the set `S` of couples `(x,y)` with `x` and `y` in `I:=\{0,1,2,3,4\}`. 

We represent an element of `S` as a 2-elements tuple, wrapped in a class 

:class:`~.set_factories_example.XYPair` deriving from :class:`ElementWrapper`. 

You can create a :class:`~.set_factories_example.XYPair` with any 

:class:`Parent`:: 

 

sage: from sage.structure.set_factories import * 

sage: from sage.structure.set_factories_example import * 

sage: p = XYPair(Parent(), (0,1)); p 

(0, 1) 

 

Now, given `(a, b)\in S` we want to consider the following subsets of 

`S` 

 

.. MATH:: 

 

S_a := \{(x,y) \in S \mid x = a\}, 

 

S^b := \{(x,y) \in S \mid y = b\}, 

 

S_a^b := \{(x,y) \in S \mid x = a, y = b\}. 

 

The constraints considered here are admittedly trivial. In a realistic 

example, there would be much more of them. And for some sets of constraints 

no good enumeration algorithms would be known. 

 

In Sage, those sets are constructed by passing the constraints to the 

factory. We first create the set with no constraints at all:: 

 

sage: XYPairs 

Factory for XY pairs 

sage: S = XYPairs(); S.list() 

[(0, 0), (1, 0), ..., (4, 0), (0, 1), (1, 1), ..., (3, 4), (4, 4)] 

sage: S.cardinality() 

25 

 

Let us construct `S_2`, `S^3` and `S_2^3`:: 

 

sage: Sx2 = XYPairs(x=2); Sx2.list() 

[(2, 0), (2, 1), (2, 2), (2, 3), (2, 4)] 

sage: Sy3 = XYPairs(y=3); Sy3.list() 

[(0, 3), (1, 3), (2, 3), (3, 3), (4, 3)] 

sage: S23 = XYPairs(x=2, y=3); S23.list() 

[(2, 3)] 

 

Set factories provide an alternative way to build subsets of an 

already constructed set: each set constructed by a factory has a 

method :meth:`~ParentWithSetFactory.subset` which accept new 

constraints. Sets constructed by the factory or the 

:meth:`~ParentWithSetFactory.subset` methods are identical:: 

 

sage: Sx2s = S.subset(x=2); Sx2 is Sx2s 

True 

sage: Sx2.subset(y=3) is S23 

True 

 

It is not possible to change an already given constraint:: 

 

sage: S23.subset(y=5) 

Traceback (most recent call last): 

... 

ValueError: Duplicate value for constraints 'y': was 3 now 5 

 

.. RUBRIC:: Constructing custom elements: policies 

 

We now come to the point of factories: constructing custom elements. The 

writer of :func:`~.set_factories_example.XYPairs` decided that, by default, 

the parents ``Sx2``, ``Sy3`` and ``S23`` are facade for parent ``S``. This 

means that each element constructed by those subsets behaves as if they where 

directly constructed by ``S`` itself:: 

 

sage: Sx2.an_element().parent() 

AllPairs 

sage: el = Sx2.an_element() 

sage: el.parent() is S 

True 

sage: type(el) is S.element_class 

True 

 

This is not always desirable. The device which decides how to construct an 

element is called a *policy* (see :class:`SetFactoryPolicy`). Each factory 

should have a default policy. Here is the policy of 

:func:`~.set_factories_example.XYPairs`:: 

 

sage: XYPairs._default_policy 

Set factory policy for <class 'sage.structure.set_factories_example.XYPair'> with parent AllPairs[=Factory for XY pairs(())] 

 

This means that with the current policy, the parent builds elements with class 

``XYPair`` and parent ``AllPairs`` which is itself constructed by calling the 

factory :func:`~.set_factories_example.XYPairs` with constraints ``()``. There 

is a lot of flexibility to change that. We now illustrate how to make a few 

different choices. 

 

1 - In a first use case, we want to add some methods to the constructed 

elements. As illustration, we add here a new method ``sum`` which returns 

`x+y`. We therefore create a new class for the elements which inherits from 

:class:`~.set_factories_example.XYPair`:: 

 

sage: class NewXYPair(XYPair): 

....: def sum(self): 

....: return sum(self.value) 

 

Here is an instance of this class (with a dummy parent):: 

 

sage: el = NewXYPair(Parent(), (2,3)) 

sage: el.sum() 

5 

 

We now want to have subsets generating those new elements while still having a 

single real parent (the one with no constraint) for each element. The 

corresponding policy is called :class:`TopMostParentPolicy`. It takes three 

parameters: 

 

- the factory; 

- the parameters for void constraint; 

- the class used for elements. 

 

Calling the factory with this policy returns a new set which builds its 

elements with the new policy:: 

 

sage: new_policy = TopMostParentPolicy(XYPairs, (), NewXYPair) 

sage: NewS = XYPairs(policy=new_policy) 

sage: el = NewS.an_element(); el 

(0, 0) 

sage: el.sum() 

0 

sage: el.parent() is NewS 

True 

sage: isinstance(el, NewXYPair) 

True 

 

Newly constructed subsets inherit the policy:: 

 

sage: NewS2 = NewS.subset(x=2) 

sage: el2 = NewS2.an_element(); el2 

(2, 0) 

sage: el2.sum() 

2 

sage: el2.parent() is NewS 

True 

 

2 - In a second use case, we want the elements to remember which 

parent created them. The corresponding policy is called 

:class:`SelfParentPolicy`. It takes only two parameters: 

 

- the factory; 

- the class used for elements. 

 

Here is an example:: 

 

sage: selfpolicy = SelfParentPolicy(XYPairs, NewXYPair) 

sage: SelfS = XYPairs(policy=selfpolicy) 

sage: el = SelfS.an_element(); 

sage: el.parent() is SelfS 

True 

 

Now all subsets are the parent of the elements that they create:: 

 

sage: SelfS2 = SelfS.subset(x=2) 

sage: el2 = SelfS2.an_element() 

sage: el2.parent() is NewS 

False 

sage: el2.parent() is SelfS2 

True 

 

3 - Finaly, a common use case is to construct simple python object which are 

not Sage :class:`sage.structure.Element`. As an example, we show how to build 

a parent ``TupleS`` which construct pairs as tuple. The corresponding policy 

is called :class:`BareFunctionPolicy`. It takes two parameters: 

 

- the factory; 

- the function called to construct the elements. 

 

Here is how to do it:: 

 

sage: cons = lambda t, check: tuple(t) # ignore the check parameter 

sage: tuplepolicy = BareFunctionPolicy(XYPairs, cons) 

sage: P = XYPairs(x=2, policy=tuplepolicy) 

sage: P.list() 

[(2, 0), (2, 1), (2, 2), (2, 3), (2, 4)] 

sage: el = P.an_element() 

sage: type(el) 

<... 'tuple'> 

 

Here are the currently implemented policies: 

 

- :class:`FacadeParentPolicy`: reuse an existing parent together with 

its element_class 

 

- :class:`TopMostParentPolicy`: use a parent created by the factory 

itself and provide a class ``Element`` for it. In this case, we need 

to specify the set of constraints which build this parent taking the 

ownership of all elements and the class which will be used for the 

``Element``. 

 

- :class:`SelfParentPolicy`: provide systematically ``Element`` and 

element_class and ensure that the parent is ``self``. 

 

- :class:`BareFunctionPolicy`: instead of calling a class constructor element 

are passed to a function provided to the policy. 

 

.. TODO:: 

 

Generalize :class:`TopMostParentPolicy` to be able to have several 

topmost parents. 

 

.. RUBRIC:: Technicalities: how policies inform parents 

 

Parents built from factories should inherit from 

:class:`ParentWithSetFactory`. This class provide a few methods 

related to factories and policies. The ``__init__`` method of 

:class:`ParentWithSetFactory` must be provided with the set of 

constraints and the policy. A parent built from a factory must create 

elements through a call to the method ``_element_constructor_``. The 

current way in which policies inform parents how to builds their elements is 

set by a few attributes. So the class must accept attribute 

adding. The precise details of which attributes are set may be subject 

to change in the future. 

 

.. RUBRIC:: How to write a set factory 

 

.. SEEALSO:: :mod:`.set_factories_example` for an example of a factory. 

 

Here are the specifications: 

 

A parent built from a factory should 

 

- *inherit* from :class:`ParentWithSetFactory`. It should accept a 

``policy`` argument and pass it verbatim to the ``__init__`` method 

of :class:`ParentWithSetFactory` together with the set of 

constraints; 

 

- *create its elements* through calls to the method 

``_element_constructor_``; 

 

- *define a method* :class:`ParentWithSetFactory.check_element` which 

checks if a built element indeed belongs to it. The method should 

accept an extra keyword parameter called ``check`` specifying which 

level of check should be performed. It will only be called when 

``bool(check)`` evaluates to ``True``. 

 

The constructor of the elements of a parent from a factory should: 

 

- receive the parent as first mandatory argument; 

 

- accept an extra optional keyword parameter called ``check`` which is meant 

to tell if the input must be checked or not. The precise meaning of 

``check`` is intentionally left vague. The only intent is that if 

``bool(check)`` evaluates to ``False``, no check is performed at all. 

 

A factory should 

 

- *define a method* ``__call__`` which is responsible for calling the 

appropriate parent constructor given the constraints; 

 

- *define a method* overloading :meth:`SetFactory.add_constraints` which is 

responsible of computing the union of two sets of constraints; 

 

- *optionally* define a method or an attribute ``_default_policy`` passed to 

the :class:`ParentWithSetFactory` if no policy is given to the factory. 

 

 

.. TODO:: 

 

There is currently no support for dealing with sets of 

constraints. The set factory and the parents must cooperate to 

consistently handle them. More support, together with a generic mechanism 

to select the appropriate parent class from the constraints, will be added 

as soon as we have gathered sufficiently enough use-cases. 

 

AUTHORS: 

 

- Florent Hivert (2011-2012): initial revision 

""" 

#***************************************************************************** 

# Copyright (C) 2012 Florent Hivert <florent.hivert at lri.fr> 

# 

# Distributed under the terms of the GNU General Public License (GPL) 

# http://www.gnu.org/licenses/ 

#***************************************************************************** 

 

 

from sage.structure.sage_object import SageObject 

from sage.structure.parent import Parent 

from sage.structure.unique_representation import UniqueRepresentation 

from sage.categories.sets_cat import Sets 

from sage.misc.abstract_method import abstract_method 

 

#################################################### 

# Factories # 

#################################################### 

 

 

class SetFactory(UniqueRepresentation, SageObject): 

r""" 

This class is currently just a stub that we will be using to add 

more structures on factories. 

 

TESTS:: 

 

sage: from sage.structure.set_factories import SetFactory 

sage: S = SetFactory() 

sage: S.__call__("foo") 

Traceback (most recent call last): 

... 

NotImplementedError: <abstract method __call__ at ...> 

sage: S.add_constraints("foo") 

Traceback (most recent call last): 

... 

NotImplementedError: <abstract method add_constraints at ...> 

""" 

@abstract_method 

def __call__(self, *constraints, **consdict): 

r""" 

Construct the parent associated with the constraints in 

argument. This should return a :class:`Parent`. 

 

.. NOTE:: 

 

Currently there is no specification on how constraints are 

passed as arguments. 

 

EXAMPLES:: 

 

sage: from sage.structure.set_factories_example import XYPairs 

sage: XYPairs() 

AllPairs 

sage: XYPairs(3) 

{(3, b) | b in range(5)} 

 

sage: XYPairs(x=3) 

{(3, b) | b in range(5)} 

 

sage: XYPairs(y=2) 

{(a, 2) | a in range(5)} 

 

TESTS:: 

 

sage: from sage.structure.set_factories import SetFactory 

sage: F = SetFactory() 

sage: F() 

Traceback (most recent call last): 

... 

NotImplementedError: <abstract method __call__ at 0x...> 

""" 

 

@abstract_method 

def add_constraints(self, cons, *args, **opts): 

r""" 

Add constraints to the set of constraints `cons`. 

 

Should return a set of constraints. 

 

.. NOTE:: 

 

Currently there is no specification on how constraints are 

passed as arguments. 

 

EXAMPLES:: 

 

sage: from sage.structure.set_factories_example import XYPairs 

sage: XYPairs.add_constraints((3,),((None, 2), {})) 

(3, 2) 

 

sage: XYPairs.add_constraints((3,),((None, None), {'y': 2})) 

(3, 2) 

 

TESTS:: 

 

sage: from sage.structure.set_factories import SetFactory 

sage: F = SetFactory() 

sage: F.add_constraints(()) 

Traceback (most recent call last): 

... 

NotImplementedError: <abstract method add_constraints at 0x...> 

""" 

 

# TODO : default policy ? 

 

#################################################### 

# Policies # 

#################################################### 

 

 

class SetFactoryPolicy(UniqueRepresentation, SageObject): 

r""" 

Abstract base class for policies. 

 

A policy is a device which is passed to a parent inheriting from 

:class:`ParentWithSetFactory` in order to set-up the element 

construction framework. 

 

INPUT: 

 

- ``factory`` -- a :class:`SetFactory` 

 

.. WARNING:: 

 

This class is a base class for policies, one should not try 

to create instances. 

""" 

def __init__(self, factory): 

r""" 

TESTS:: 

 

sage: from sage.structure.set_factories import SetFactoryPolicy 

sage: from sage.structure.set_factories_example import XYPairs 

sage: S = SetFactoryPolicy(XYPairs); S 

<sage.structure.set_factories.SetFactoryPolicy object at ...> 

""" 

assert isinstance(factory, SetFactory) 

self._factory = factory 

 

def factory(self): 

r""" 

Return the factory for ``self``. 

 

EXAMPLES:: 

 

sage: from sage.structure.set_factories import SetFactoryPolicy, SelfParentPolicy 

sage: from sage.structure.set_factories_example import XYPairs, XYPair 

sage: XYPairs._default_policy.factory() 

Factory for XY pairs 

sage: XYPairs._default_policy.factory() is XYPairs 

True 

 

TESTS:: 

 

sage: policy = SetFactoryPolicy(XYPairs) 

sage: policy.factory() 

Factory for XY pairs 

sage: SelfParentPolicy(XYPairs, XYPair).factory() 

Factory for XY pairs 

""" 

return self._factory 

 

def self_element_constructor_attributes(self, Element): 

r""" 

Element Constructor Attributes for non facade parent. 

 

The list of attributes which must be set during the init of a 

non facade parent with factory. 

 

INPUT: 

 

- ``Element`` -- the class used for the elements 

 

EXAMPLES:: 

 

sage: from sage.structure.set_factories_example import XYPairs, XYPair 

sage: pol = XYPairs._default_policy 

sage: pol.self_element_constructor_attributes(XYPair) 

{'Element': <class 'sage.structure.set_factories_example.XYPair'>, 

'_parent_for': 'self'} 

""" 

return {'_parent_for': "self", 'Element': Element} 

 

def facade_element_constructor_attributes(self, parent): 

r""" 

Element Constructor Attributes for facade parent. 

 

The list of attributes which must be set during the init of a 

facade parent with factory. 

 

INPUT: 

 

- ``parent`` -- the actual parent for the elements 

 

EXAMPLES:: 

 

sage: from sage.structure.set_factories_example import XYPairs, XYPair 

sage: pol = XYPairs._default_policy 

sage: pol.facade_element_constructor_attributes(XYPairs()) 

{'_facade_for': AllPairs, 

'_parent_for': AllPairs, 

'element_class': <class 'sage.structure.set_factories_example.AllPairs_with_category.element_class'>} 

""" 

return {'_parent_for': parent, 

'_facade_for': parent, 

'element_class': parent.element_class} 

 

@abstract_method 

def element_constructor_attributes(self, constraints): 

r""" 

Element constructor attributes. 

 

INPUT: 

 

- ``constraints`` -- a bunch of constraints 

 

Should return the attributes that are prerequisite for element 

construction. This is coordinated with 

:meth:`ParentWithSetFactory._element_constructor_`. Currently two standard 

attributes are provided in 

:meth:`facade_element_constructor_attributes` and 

:meth:`self_element_constructor_attributes`. You should return the 

one needed depending on the given constraints. 

 

EXAMPLES:: 

 

sage: from sage.structure.set_factories_example import XYPairs, XYPair 

sage: pol = XYPairs._default_policy 

sage: pol.element_constructor_attributes(()) 

{'Element': <class 'sage.structure.set_factories_example.XYPair'>, 

'_parent_for': 'self'} 

sage: pol.element_constructor_attributes((1)) 

{'_facade_for': AllPairs, 

'_parent_for': AllPairs, 

'element_class': <class 'sage.structure.set_factories_example.AllPairs_with_category.element_class'>} 

""" 

 

 

class SelfParentPolicy(SetFactoryPolicy): 

r""" 

Policy where each parent is a standard parent. 

 

INPUT: 

 

- ``factory`` -- an instance of :class:`SetFactory` 

- ``Element`` -- a subclass of :class:`~.element.Element` 

 

Given a factory ``F`` and a class ``E``, returns a policy for 

parent ``P`` creating elements in class ``E`` and parent ``P`` 

itself. 

 

EXAMPLES:: 

 

sage: from sage.structure.set_factories import SelfParentPolicy 

sage: from sage.structure.set_factories_example import XYPairs, XYPair, Pairs_Y 

sage: pol = SelfParentPolicy(XYPairs, XYPair) 

sage: S = Pairs_Y(3, pol) 

sage: el = S.an_element() 

sage: el.parent() is S 

True 

 

sage: class Foo(XYPair): pass 

sage: pol = SelfParentPolicy(XYPairs, Foo) 

sage: S = Pairs_Y(3, pol) 

sage: el = S.an_element() 

sage: isinstance(el, Foo) 

True 

""" 

def __init__(self, factory, Element): 

r""" 

TESTS:: 

 

sage: from sage.structure.set_factories import SelfParentPolicy 

sage: from sage.structure.set_factories_example import XYPairs, XYPair 

sage: S = SelfParentPolicy(XYPairs, XYPair); S 

Set factory policy for <class 'sage.structure.set_factories_example.XYPair'> with parent ``self`` 

sage: TestSuite(S).run(skip='_test_category') 

""" 

self._Element = Element 

SetFactoryPolicy.__init__(self, factory) 

 

def element_constructor_attributes(self, constraints): 

r""" 

Return the element constructor attributes as per 

:meth:`SetFactoryPolicy.element_constructor_attributes` 

 

INPUT: 

 

- ``constraints`` -- a bunch of constraints 

 

TESTS:: 

 

sage: from sage.structure.set_factories import SelfParentPolicy 

sage: from sage.structure.set_factories_example import XYPairs, XYPair 

sage: pol = SelfParentPolicy(XYPairs, XYPair) 

sage: pol.element_constructor_attributes(()) 

{'Element': <class 'sage.structure.set_factories_example.XYPair'>, 

'_parent_for': 'self'} 

""" 

return self.self_element_constructor_attributes(self._Element) 

 

def _repr_(self): 

r""" 

TESTS:: 

 

sage: from sage.structure.set_factories import SelfParentPolicy 

sage: from sage.structure.set_factories_example import XYPairs, XYPair 

sage: SelfParentPolicy(XYPairs, XYPair) # indirect doctest 

Set factory policy for <class 'sage.structure.set_factories_example.XYPair'> with parent ``self`` 

""" 

return "Set factory policy for {} with parent ``self``".format(self._Element) 

 

 

class TopMostParentPolicy(SetFactoryPolicy): 

r""" 

Policy where the parent of the elements is the topmost parent. 

 

INPUT: 

 

- ``factory`` -- an instance of :class:`SetFactory` 

- ``top_constraints`` -- the empty set of constraints. 

- ``Element`` -- a subclass of :class:`~.element.Element` 

 

Given a factory ``F`` and a class ``E``, returns a policy for 

parent ``P`` creating element in class ``E`` and parent 

``factory(*top_constraints, policy)``. 

 

EXAMPLES:: 

 

sage: from sage.structure.set_factories_example import XYPairs, XYPair 

sage: P = XYPairs(); P.policy() 

Set factory policy for <class 'sage.structure.set_factories_example.XYPair'> with parent AllPairs[=Factory for XY pairs(())] 

""" 

def __init__(self, factory, top_constraints, Element): 

""" 

TESTS:: 

 

sage: from sage.structure.set_factories import TopMostParentPolicy 

sage: from sage.structure.set_factories_example import XYPairs, XYPair 

sage: T = TopMostParentPolicy(XYPairs, (), XYPair); T 

Set factory policy for <class 'sage.structure.set_factories_example.XYPair'> with parent AllPairs[=Factory for XY pairs(())] 

sage: TestSuite(T).run(skip='_test_category') 

""" 

# assert(isinstance(top_constraints, tuple)) 

self._top_constraints = top_constraints 

self._Element = Element 

SetFactoryPolicy.__init__(self, factory) 

 

def element_constructor_attributes(self, constraints): 

r""" 

Return the element constructor attributes as per 

:meth:`SetFactoryPolicy.element_constructor_attributes`. 

 

INPUT: 

 

- ``constraints`` -- a bunch of constraints 

 

TESTS:: 

 

sage: from sage.structure.set_factories import TopMostParentPolicy 

sage: from sage.structure.set_factories_example import XYPairs, XYPair 

sage: pol = TopMostParentPolicy(XYPairs, (), XYPair) 

sage: pol.element_constructor_attributes(()) 

{'Element': <class 'sage.structure.set_factories_example.XYPair'>, 

'_parent_for': 'self'} 

sage: pol.element_constructor_attributes((1)) 

{'_facade_for': AllPairs, 

'_parent_for': AllPairs, 

'element_class': <class 'sage.structure.set_factories_example.AllPairs_with_category.element_class'>} 

""" 

factory = self._factory 

if constraints == self._top_constraints: 

return self.self_element_constructor_attributes(self._Element) 

else: 

return self.facade_element_constructor_attributes( 

factory(*self._top_constraints, policy=self)) 

 

def _repr_(self): 

r""" 

TESTS:: 

 

sage: from sage.structure.set_factories import TopMostParentPolicy 

sage: from sage.structure.set_factories_example import XYPairs, XYPair 

sage: TopMostParentPolicy(XYPairs, (), XYPair) # indirect doctest 

Set factory policy for <class 'sage.structure.set_factories_example.XYPair'> with parent AllPairs[=Factory for XY pairs(())] 

""" 

return "Set factory policy for {} with parent {}[={}({})]".format( 

self._Element, self._factory(*self._top_constraints, policy=self), 

self._factory, self._top_constraints) 

 

 

class FacadeParentPolicy(SetFactoryPolicy): 

r""" 

Policy for facade parent. 

 

INPUT: 

 

- ``factory`` -- an instance of :class:`SetFactory` 

- ``parent`` -- an instance of :class:`Parent` 

 

Given a factory ``F`` and a class ``E``, returns a policy for 

parent ``P`` creating elements as if they were created by 

``parent``. 

 

EXAMPLES:: 

 

sage: from sage.structure.set_factories import SelfParentPolicy, FacadeParentPolicy 

sage: from sage.structure.set_factories_example import XYPairs, XYPair 

 

We create a custom standard parent ``P``:: 

 

sage: selfpolicy = SelfParentPolicy(XYPairs, XYPair) 

sage: P = XYPairs(x=2, policy=selfpolicy) 

sage: pol = FacadeParentPolicy(XYPairs, P) 

sage: P2 = XYPairs(x=2, y=3, policy=pol) 

sage: el = P2.an_element() 

sage: el.parent() is P 

True 

sage: type(el) is P.element_class 

True 

 

If ``parent`` is itself a facade parent, then transitivity is 

correctly applied:: 

 

sage: P = XYPairs() 

sage: P2 = XYPairs(x=2) 

sage: P2.category() 

Category of facade finite enumerated sets 

sage: pol = FacadeParentPolicy(XYPairs, P) 

sage: P23 = XYPairs(x=2, y=3, policy=pol) 

sage: el = P2.an_element() 

sage: el.parent() is P 

True 

sage: type(el) is P.element_class 

True 

""" 

def __init__(self, factory, parent): 

r""" 

TESTS:: 

 

sage: from sage.structure.set_factories import FacadeParentPolicy 

sage: from sage.structure.set_factories_example import XYPairs, XYPair 

sage: F = FacadeParentPolicy(XYPairs, XYPairs()); F 

Set factory policy for facade parent AllPairs 

sage: TestSuite(F).run(skip='_test_category') 

""" 

self._parent_for = parent 

SetFactoryPolicy.__init__(self, factory) 

 

def element_constructor_attributes(self, constraints): 

r""" 

Return the element constructor attributes as per 

:meth:`SetFactoryPolicy.element_constructor_attributes`. 

 

INPUT: 

 

- ``constraints`` -- a bunch of constraints 

 

TESTS:: 

 

sage: from sage.structure.set_factories import FacadeParentPolicy 

sage: from sage.structure.set_factories_example import XYPairs, XYPair 

sage: pol = FacadeParentPolicy(XYPairs, XYPairs()) 

sage: pol.element_constructor_attributes(()) 

{'_facade_for': AllPairs, 

'_parent_for': AllPairs, 

'element_class': <class 'sage.structure.set_factories_example.AllPairs_with_category.element_class'>} 

sage: pol.element_constructor_attributes((1)) 

{'_facade_for': AllPairs, 

'_parent_for': AllPairs, 

'element_class': <class 'sage.structure.set_factories_example.AllPairs_with_category.element_class'>} 

""" 

return self.facade_element_constructor_attributes( 

self._parent_for._parent_for) 

 

def _repr_(self): 

r""" 

TESTS:: 

 

sage: from sage.structure.set_factories import FacadeParentPolicy 

sage: from sage.structure.set_factories_example import XYPairs, XYPair 

sage: FacadeParentPolicy(XYPairs, XYPairs()) # indirect doctest 

Set factory policy for facade parent AllPairs 

""" 

return "Set factory policy for facade parent {}".format( 

self._parent_for) 

 

 

 

 

class BareFunctionPolicy(SetFactoryPolicy): 

r""" 

Policy where element are constructed using a bare function. 

 

INPUT: 

 

- ``factory`` -- an instance of :class:`SetFactory` 

- ``constructor`` -- a function 

 

Given a factory ``F`` and a function ``c``, returns a policy for 

parent ``P`` creating element using the function ``f``. 

 

EXAMPLES:: 

 

sage: from sage.structure.set_factories import BareFunctionPolicy 

sage: from sage.structure.set_factories_example import XYPairs 

sage: cons = lambda t, check: tuple(t) # ignore the check parameter 

sage: tuplepolicy = BareFunctionPolicy(XYPairs, cons) 

sage: P = XYPairs(x=2, policy=tuplepolicy) 

sage: el = P.an_element() 

sage: type(el) 

<... 'tuple'> 

""" 

def __init__(self, factory, constructor): 

""" 

TESTS:: 

 

sage: from sage.structure.set_factories import BareFunctionPolicy 

sage: from sage.structure.set_factories_example import XYPairs 

sage: pol = BareFunctionPolicy(XYPairs, tuple) 

sage: TestSuite(pol).run(skip='_test_category') 

""" 

# assert(isinstance(top_constraints, tuple)) 

self._constructor = constructor 

SetFactoryPolicy.__init__(self, factory) 

 

def element_constructor_attributes(self, constraints): 

r""" 

Return the element constructor attributes as per 

:meth:`SetFactoryPolicy.element_constructor_attributes`. 

 

INPUT: 

 

- ``constraints`` -- a bunch of constraints 

 

TESTS:: 

 

sage: from sage.structure.set_factories import BareFunctionPolicy 

sage: from sage.structure.set_factories_example import XYPairs 

sage: pol = BareFunctionPolicy(XYPairs, tuple) 

sage: pol.element_constructor_attributes(()) 

{'_element_constructor_': <... 'tuple'>, '_parent_for': None} 

""" 

return {'_element_constructor_' : self._constructor, '_parent_for' : None} 

 

def _repr_(self): 

r""" 

TESTS:: 

 

sage: from sage.structure.set_factories import BareFunctionPolicy 

sage: from sage.structure.set_factories_example import XYPairs 

sage: BareFunctionPolicy(XYPairs, tuple) 

Set factory policy for bare function <... 'tuple'> 

""" 

return "Set factory policy for bare function {}".format(self._constructor) 

 

 

#################################################### 

# Parent # 

#################################################### 

 

 

class ParentWithSetFactory(Parent): 

r""" 

Abstract class for parent belonging to a set factory. 

 

INPUT: 

 

- ``constraints`` -- a set of constraints 

- ``policy`` -- the policy for element construction 

- ``category`` -- the category of the parent (default to ``None``) 

 

Depending on the constraints and the policy, initialize the parent 

in a proper category to set up element construction. 

 

EXAMPLES:: 

 

sage: from sage.structure.set_factories_example import XYPairs, PairsX_ 

sage: P = PairsX_(3, XYPairs._default_policy) 

sage: P is XYPairs(3) 

True 

sage: P.category() 

Category of facade finite enumerated sets 

""" 

def __init__(self, constraints, policy, category=None): 

r""" 

TESTS:: 

 

sage: from sage.structure.set_factories import ParentWithSetFactory 

sage: from sage.structure.set_factories_example import XYPairs 

sage: isinstance(XYPairs(3), ParentWithSetFactory) # indirect doctest 

True 

""" 

self._constraints = constraints 

assert isinstance(policy, SetFactoryPolicy) 

self._policy = policy 

policy_attributes = policy.element_constructor_attributes(constraints) 

for attrname, attr in policy_attributes.items(): 

if attr == "self": 

setattr(self, attrname, self) 

else: 

setattr(self, attrname, attr) 

assert self._parent_for is None or isinstance(self._parent_for, Parent) 

Parent.__init__(self, 

category=category, 

facade=policy_attributes.get('_facade_for', None)) 

 

def constraints(self): 

r""" 

Return the constraints defining ``self``. 

 

.. NOTE:: 

 

Currently there is no specification on how constraints are 

passed as arguments. 

 

EXAMPLES:: 

 

sage: from sage.structure.set_factories_example import XYPairs 

sage: XYPairs().constraints() 

() 

sage: XYPairs(x=3).constraints() 

(3, None) 

sage: XYPairs(y=2).constraints() 

(None, 2) 

""" 

return self._constraints 

 

def policy(self): 

r""" 

Return the policy used when ``self`` was created. 

 

EXAMPLES:: 

 

sage: from sage.structure.set_factories_example import XYPairs 

sage: XYPairs().policy() 

Set factory policy for <class 'sage.structure.set_factories_example.XYPair'> with parent AllPairs[=Factory for XY pairs(())] 

sage: XYPairs(x=3).policy() 

Set factory policy for <class 'sage.structure.set_factories_example.XYPair'> with parent AllPairs[=Factory for XY pairs(())] 

""" 

return self._policy 

 

def facade_policy(self): 

r""" 

Return the policy for parent facade for ``self``. 

 

EXAMPLES:: 

 

sage: from sage.structure.set_factories import SelfParentPolicy 

sage: from sage.structure.set_factories_example import XYPairs, XYPair 

 

We create a custom standard parent ``P``:: 

 

sage: selfpolicy = SelfParentPolicy(XYPairs, XYPair) 

sage: P = XYPairs(x=2, policy=selfpolicy) 

sage: P.facade_policy() 

Set factory policy for facade parent {(2, b) | b in range(5)} 

 

Now passing ``P.facade_policy()`` creates parent which are facade for 

``P``:: 

 

sage: P3 = XYPairs(x=2, y=3, policy=P.facade_policy()) 

sage: P3.facade_for() == (P,) 

True 

sage: el = P3.an_element() 

sage: el.parent() is P 

True 

""" 

return FacadeParentPolicy(self.factory(), self) 

 

def factory(self): 

r""" 

Return the factory which built ``self``. 

 

EXAMPLES:: 

 

sage: from sage.structure.set_factories_example import XYPairs 

sage: XYPairs().factory() is XYPairs 

True 

sage: XYPairs(x=3).factory() is XYPairs 

True 

""" 

return self._policy.factory() 

 

def subset(self, *args, **options): 

r""" 

Return a subset of ``self`` by adding more constraints. 

 

.. NOTE:: 

 

Currently there is no specification on how constraints are 

passed as arguments. 

 

EXAMPLES:: 

 

sage: from sage.structure.set_factories_example import XYPairs 

sage: S = XYPairs() 

sage: S3 = S.subset(x=3) 

sage: S3.list() 

[(3, 0), (3, 1), (3, 2), (3, 3), (3, 4)] 

 

TESTS:: 

 

sage: S3 is XYPairs(3) 

True 

sage: S3 is XYPairs(x=3) 

True 

""" 

factory = self.factory() 

constr = factory.add_constraints(self._constraints, 

(args, options)) 

return factory(*constr, policy=self._policy) 

 

def _test_subset(self, **options): 

r""" 

Tests that subsets with no extra parameters returns 

``self``. 

 

Currently, only the test that one gets the same parent when no 

more constraints are given, is performed. 

 

.. TODO:: 

 

Straighten the test when handling of constraints will be 

specified. 

 

EXAMPLES:: 

 

sage: from sage.structure.set_factories_example import XYPairs 

sage: S = XYPairs() 

sage: S._test_subset() 

""" 

tester = self._tester(**options) 

tester.assertTrue(self.subset() is self) 

 

@abstract_method 

def check_element(self, x, check): 

r""" 

Check that ``x`` verifies the constraints of ``self``. 

 

INPUT: 

 

- ``x`` -- an instance of ``self.element_class``. 

 

- ``check`` -- the level of checking to be performed (usually a 

boolean). 

 

This method may assume that ``x`` was properly constructed by 

``self`` or a possible super-set of ``self`` for which 

``self`` is a facade. It should return nothing if ``x`` 

verifies the constraints and raise a 

:exc:`~exceptions.ValueError` explaining which constraints 

``x`` fails otherwise. 

 

The method should accept an extra parameter check specifying 

which level of check should be performed. It will only be 

called when ``bool(check)`` evaluates to ``True``. 

 

.. TODO:: 

 

Should we always call check element and let it decide 

which check has to be performed ? 

 

EXAMPLES:: 

 

sage: from sage.structure.set_factories_example import XYPairs 

sage: S = XYPairs() 

sage: el = S((2,3)) 

sage: S.check_element(el, True) 

sage: XYPairs(x=2).check_element(el, True) 

sage: XYPairs(x=3).check_element(el, True) 

Traceback (most recent call last): 

... 

ValueError: Wrong first coordinate 

sage: XYPairs(y=4).check_element(el, True) 

Traceback (most recent call last): 

... 

ValueError: Wrong second coordinate 

""" 

 

def __contains__(self, x): 

r""" 

Default implementation for ``__contains__``. 

 

INPUT: 

 

- ``x`` -- any object 

 

Check for class, parent and calls ``self.check_element(x)``. 

 

TESTS:: 

 

sage: from sage.structure.set_factories_example import XYPairs 

sage: S = XYPairs() 

sage: el = S((2,3)) 

sage: el in S 

True 

sage: el in XYPairs(x=2) 

True 

sage: el in XYPairs(x=3) 

False 

sage: el in XYPairs(y=4) 

False 

""" 

if (isinstance(x, self.element_class) and 

x.parent() == self._parent_for): # TODO: is_parent_of ??? 

try: 

self.check_element(x, True) 

except ValueError: 

return False 

else: 

return True 

return False 

 

def __call__(self, *args, **keywords): 

r""" 

TESTS:: 

 

sage: from sage.structure.set_factories_example import XYPairs 

sage: S = XYPairs() 

sage: el = S((2,3)); el 

(2, 3) 

sage: S(el) is el 

True 

 

sage: XYPairs(x=3)((2,3)) 

Traceback (most recent call last): 

... 

ValueError: Wrong first coordinate 

 

sage: XYPairs(x=3)(el) 

Traceback (most recent call last): 

... 

ValueError: Wrong first coordinate 

""" 

# Ensure idempotence of element construction 

if (len(args) == 1 and 

isinstance(args[0], self.element_class) and 

args[0].parent() == self._parent_for): 

check = keywords.get("check", True) 

if check: 

self.check_element(args[0], check) 

return args[0] 

else: 

return Parent.__call__(self, *args, **keywords) 

 

# QUESTION: Should we call: 

# self._parent_for._element_constructor_ 

# currently we do not call it directly because: 

# - it may do some extra check we dont want to perform ? 

# - calling directly element_class should be faster 

def _element_constructor_(self, *args, **keywords): 

r""" 

TESTS:: 

 

sage: from sage.structure.set_factories_example import XYPairs 

sage: XYPairs()((2,3)) # indirect doctest 

(2, 3) 

sage: XYPairs(x=3)((3,3)) # indirect doctest 

(3, 3) 

sage: XYPairs(x=3)((2,3)) # indirect doctest 

Traceback (most recent call last): 

... 

ValueError: Wrong first coordinate 

 

sage: XYPairs(x=3)((2,3), check=False) # Don't do this at home, kids 

(2, 3) 

""" 

check = keywords.get("check", True) 

res = self.element_class(self._parent_for, *args, **keywords) 

if check: 

self.check_element(res, check) 

return res