switchover to rspace next_step and dump step()
[scpubgit/DX.git] / lib / DX / Utils.pm
1 package DX::Utils;
2
3 use strictures 2;
4 use Exporter 'import';
5
6 my @const = (
7   my @dep_types = qw(EXISTENCE_OF TYPE_OF INDICES_OF CONTENTS_OF),
8   my @ev_types = qw(VALUE_SET VALUE_EXISTS),
9 );
10
11 our @EXPORT_OK = (
12   @const,
13   (my @builders = qw(rspace rstrat res string number dict proposition)),
14   'deparse', '*trace',
15 );
16
17 our %EXPORT_TAGS = (
18   all => \@EXPORT_OK,
19   dep_types => \@dep_types,
20   event_types => \@ev_types,
21   builders => \@builders,
22 );
23
24 require constant;
25
26 # use constant INDICES_OF => \*INDICES_OF;
27
28 constant->import(+{
29   map {; no strict 'refs'; $_ => \*$_ } @const
30 });
31
32 # $EXISTENCE_OF = 1, ...
33
34 do { no strict 'refs'; ${$dep_types[$_-1]} = $_ } for 1..@dep_types;
35
36 # VALUE_EXISTS needs to trigger indices checks on its parent
37
38 our $VALUE_EXISTS = 1;
39
40 # VALUE_EXISTS triggers all types, VALUE_SET all but EXISTENCE_OF
41
42 our @VALUE_EXISTS = (EXISTENCE_OF(), TYPE_OF(), INDICES_OF(), CONTENTS_OF());
43 our @VALUE_SET = (TYPE_OF(), INDICES_OF(), CONTENTS_OF());
44
45 sub trace { }
46
47 sub _expand_dep {
48   my ($type, @path) = @{$_[0]};
49   my @expanded = map {
50     ref() ? @{$_->value_path or return ()} : $_
51   } @path;
52   return [ $type, @expanded ];
53 }
54
55 sub _expand_deps {
56   [ map _expand_dep($_), @{$_[0]} ]
57 }
58
59 sub rspace {
60   require DX::ResolutionSpace;
61   DX::ResolutionSpace->new(@_);
62 }
63
64 sub rstrat {
65   require DX::ResolutionStrategy;
66   DX::ResolutionStrategy->new(@_);
67 }
68
69 sub res {
70   require DX::Resolution;
71   DX::Resolution->new(@_);
72 }
73
74 sub string {
75   require DX::Value::String;
76   DX::Value::String->new(string_value => $_[0])
77 }
78
79 sub number {
80   require DX::Value::Number;
81   DX::Value::Number->new(number_value => $_[0]);
82 }
83
84 sub dict {
85   require DX::Value::Dict;
86   DX::Value::Dict->new(
87     members => { @_ },
88   );
89 }
90
91 sub proposition {
92   my ($pred, @args) = @_;
93   require DX::Proposition;
94   DX::Proposition->new(
95     predicate => $pred,
96     args => \@args,
97   );
98 }
99
100 {
101   my $dp;
102
103   sub deparse {
104     $dp ||= do {
105       require DX::TraceFormatter;
106       DX::TraceFormatter->new;
107     };
108     my ($thing) = @_;
109     $dp->format($thing);
110   }
111 }
112
113 1;