3v4l.org

run code in 300+ PHP versions simultaneously
<?php #BlackLivesMatter - github.com/ghostwriter namespace PolynomialFunctors; use InvalidArgumentException; /** * Represents a polynomial functor of the form `1 + x + x^2`. * * @template T * @implements Functor<T> */ final class Polynomial implements Functor { private int $tag; private mixed $value; private function __construct(int $tag, mixed $value = null) { $this->tag = $tag; $this->value = $value; } /** * Represents the `1` case. * * @return self */ public static function unit(): self { return new self(0); } /** * Represents the `x` case with one value. * * @template T * @param T $value * @return self<T> */ public static function linear(mixed $value): self { return new self(1, $value); } /** * Represents the `x^2` case with two values. * * @template T * @param T $first * @param T $second * @return self<T> */ public static function quadratic(mixed $first, mixed $second): self { return new self(2, [$first, $second]); } /** * Maps a function over the polynomial structure. * * @template A * @template B * @param callable(A): B $f * @return self<B> */ public function map(callable $f): Polynomial { return match ($this->tag) { 0 => self::unit(), 1 => self::linear($f($this->value)), 2 => self::quadratic($f($this->value[0]), $f($this->value[1])), default => throw new InvalidArgumentException('Unknown polynomial tag') }; } } interface Functor { /** * Maps a function over the functor. * * @template A * @template B * @param callable(A): B $f * @return Functor<B> */ public function map(callable $f): Functor; } $unit = Polynomial::unit(); // Represents `1` $linear = Polynomial::linear(5); // Represents `x` $quadratic = Polynomial::quadratic(3, 7); // Represents `x^2` // Mapping over the functor $mappedLinear = $linear->map(fn(int $x): int => $x * 2); // Maps over the single value $mappedQuadratic = $quadratic->map(fn(int $x): int => $x + 1); // Maps over both values // Output results var_dump($unit); // Nothing to map var_dump($mappedLinear); // Polynomial with value 10 var_dump($mappedQuadratic); // Polynomial with values 4 and 8
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/aEnTYH
function name:  (null)
number of ops:  33
compiled vars:  !0 = $unit, !1 = $linear, !2 = $quadratic, !3 = $mappedLinear, !4 = $mappedQuadratic
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   13     0  E >   DECLARE_CLASS                                            'polynomialfunctors%5Cpolynomial'
   91     1        INIT_STATIC_METHOD_CALL                                  'PolynomialFunctors%5CPolynomial', 'unit'
          2        DO_FCALL                                      0  $5      
          3        ASSIGN                                                   !0, $5
   92     4        INIT_STATIC_METHOD_CALL                                  'PolynomialFunctors%5CPolynomial', 'linear'
          5        SEND_VAL_EX                                              5
          6        DO_FCALL                                      0  $7      
          7        ASSIGN                                                   !1, $7
   93     8        INIT_STATIC_METHOD_CALL                                  'PolynomialFunctors%5CPolynomial', 'quadratic'
          9        SEND_VAL_EX                                              3
         10        SEND_VAL_EX                                              7
         11        DO_FCALL                                      0  $9      
         12        ASSIGN                                                   !2, $9
   96    13        INIT_METHOD_CALL                                         !1, 'map'
         14        DECLARE_LAMBDA_FUNCTION                          ~11     [0]
         15        SEND_VAL_EX                                              ~11
         16        DO_FCALL                                      0  $12     
         17        ASSIGN                                                   !3, $12
   97    18        INIT_METHOD_CALL                                         !2, 'map'
         19        DECLARE_LAMBDA_FUNCTION                          ~14     [1]
         20        SEND_VAL_EX                                              ~14
         21        DO_FCALL                                      0  $15     
         22        ASSIGN                                                   !4, $15
  100    23        INIT_NS_FCALL_BY_NAME                                    'PolynomialFunctors%5Cvar_dump'
         24        SEND_VAR_EX                                              !0
         25        DO_FCALL                                      0          
  101    26        INIT_NS_FCALL_BY_NAME                                    'PolynomialFunctors%5Cvar_dump'
         27        SEND_VAR_EX                                              !3
         28        DO_FCALL                                      0          
  102    29        INIT_NS_FCALL_BY_NAME                                    'PolynomialFunctors%5Cvar_dump'
         30        SEND_VAR_EX                                              !4
         31        DO_FCALL                                      0          
         32      > RETURN                                                   1


Dynamic Functions:
Dynamic Function 0
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/aEnTYH
function name:  PolynomialFunctors\{closure}
number of ops:  6
compiled vars:  !0 = $x
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   96     0  E >   RECV                                             !0      
          1        MUL                                              ~1      !0, 2
          2        VERIFY_RETURN_TYPE                                       ~1
          3      > RETURN                                                   ~1
          4*       VERIFY_RETURN_TYPE                                       
          5*     > RETURN                                                   null

End of Dynamic Function 0

Dynamic Function 1
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/aEnTYH
function name:  PolynomialFunctors\{closure}
number of ops:  6
compiled vars:  !0 = $x
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   97     0  E >   RECV                                             !0      
          1        ADD                                              ~1      !0, 1
          2        VERIFY_RETURN_TYPE                                       ~1
          3      > RETURN                                                   ~1
          4*       VERIFY_RETURN_TYPE                                       
          5*     > RETURN                                                   null

End of Dynamic Function 1

Class PolynomialFunctors\Polynomial:
Function __construct:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/aEnTYH
function name:  __construct
number of ops:  7
compiled vars:  !0 = $tag, !1 = $value
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   18     0  E >   RECV                                             !0      
          1        RECV_INIT                                        !1      null
   20     2        ASSIGN_OBJ                                               'tag'
          3        OP_DATA                                                  !0
   21     4        ASSIGN_OBJ                                               'value'
          5        OP_DATA                                                  !1
   22     6      > RETURN                                                   null

End of function __construct

Function unit:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/aEnTYH
function name:  unit
number of ops:  7
compiled vars:  none
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   31     0  E >   NEW                          self                $0      
          1        SEND_VAL_EX                                              0
          2        DO_FCALL                                      0          
          3        VERIFY_RETURN_TYPE                                       $0
          4      > RETURN                                                   $0
   32     5*       VERIFY_RETURN_TYPE                                       
          6*     > RETURN                                                   null

End of function unit

Function linear:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/aEnTYH
function name:  linear
number of ops:  9
compiled vars:  !0 = $value
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   41     0  E >   RECV                                             !0      
   43     1        NEW                          self                $1      
          2        SEND_VAL_EX                                              1
          3        SEND_VAR_EX                                              !0
          4        DO_FCALL                                      0          
          5        VERIFY_RETURN_TYPE                                       $1
          6      > RETURN                                                   $1
   44     7*       VERIFY_RETURN_TYPE                                       
          8*     > RETURN                                                   null

End of function linear

Function quadratic:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/aEnTYH
function name:  quadratic
number of ops:  12
compiled vars:  !0 = $first, !1 = $second
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   54     0  E >   RECV                                             !0      
          1        RECV                                             !1      
   56     2        NEW                          self                $2      
          3        SEND_VAL_EX                                              2
          4        INIT_ARRAY                                       ~3      !0
          5        ADD_ARRAY_ELEMENT                                ~3      !1
          6        SEND_VAL_EX                                              ~3
          7        DO_FCALL                                      0          
          8        VERIFY_RETURN_TYPE                                       $2
          9      > RETURN                                                   $2
   57    10*       VERIFY_RETURN_TYPE                                       
         11*     > RETURN                                                   null

End of function quadratic

Function map:
Finding entry points
Branch analysis from position: 0
4 jumps found. (Code = 195) Position 1 = 3, Position 2 = 7, Position 3 = 17, Position 4 = 35
Branch analysis from position: 3
1 jumps found. (Code = 42) Position 1 = 41
Branch analysis from position: 41
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 7
1 jumps found. (Code = 42) Position 1 = 41
Branch analysis from position: 41
Branch analysis from position: 17
1 jumps found. (Code = 42) Position 1 = 41
Branch analysis from position: 41
Branch analysis from position: 35
1 jumps found. (Code = 108) Position 1 = -2
filename:       /in/aEnTYH
function name:  map
number of ops:  46
compiled vars:  !0 = $f
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   67     0  E >   RECV                                             !0      
   69     1        FETCH_OBJ_R                                      ~1      'tag'
          2      > MATCH                                                    ~1, [ 0:->3, 1:->7, 2:->17, ], ->35
   70     3    >   INIT_STATIC_METHOD_CALL                                  'unit'
          4        DO_FCALL                                      0  $3      
          5        QM_ASSIGN                                        ~4      $3
          6      > JMP                                                      ->41
   71     7    >   INIT_STATIC_METHOD_CALL                                  'linear'
          8        INIT_DYNAMIC_CALL                                        !0
          9        CHECK_FUNC_ARG                                           
         10        FETCH_OBJ_FUNC_ARG                               $5      'value'
         11        SEND_FUNC_ARG                                            $5
         12        DO_FCALL                                      0  $6      
         13        SEND_VAR                                                 $6
         14        DO_FCALL                                      0  $7      
         15        QM_ASSIGN                                        ~4      $7
         16      > JMP                                                      ->41
   72    17    >   INIT_STATIC_METHOD_CALL                                  'quadratic'
         18        INIT_DYNAMIC_CALL                                        !0
         19        CHECK_FUNC_ARG                                           
         20        FETCH_OBJ_FUNC_ARG                               $8      'value'
         21        FETCH_DIM_FUNC_ARG                               $9      $8, 0
         22        SEND_FUNC_ARG                                            $9
         23        DO_FCALL                                      0  $10     
         24        SEND_VAR                                                 $10
         25        INIT_DYNAMIC_CALL                                        !0
         26        CHECK_FUNC_ARG                                           
         27        FETCH_OBJ_FUNC_ARG                               $11     'value'
         28        FETCH_DIM_FUNC_ARG                               $12     $11, 1
         29        SEND_FUNC_ARG                                            $12
         30        DO_FCALL                                      0  $13     
         31        SEND_VAR                                                 $13
         32        DO_FCALL                                      0  $14     
         33        QM_ASSIGN                                        ~4      $14
         34      > JMP                                                      ->41
   73    35    >   NEW                                              $15     'InvalidArgumentException'
         36        SEND_VAL_EX                                              'Unknown+polynomial+tag'
         37        DO_FCALL                                      0          
         38      > THROW                                         1          $15
         39*       QM_ASSIGN                                        ~4      <true>
         40*       JMP                                                      ->41
         41    >   FREE                                                     ~1
         42        VERIFY_RETURN_TYPE                                       ~4
         43      > RETURN                                                   ~4
   75    44*       VERIFY_RETURN_TYPE                                       
         45*     > RETURN                                                   null

End of function map

End of class PolynomialFunctors\Polynomial.

Class PolynomialFunctors\Functor:
Function map:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/aEnTYH
function name:  map
number of ops:  3
compiled vars:  !0 = $f
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   88     0  E >   RECV                                             !0      
          1        VERIFY_RETURN_TYPE                                       
          2      > RETURN                                                   null

End of function map

End of class PolynomialFunctors\Functor.

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
153.93 ms | 1018 KiB | 14 Q