├── .gitignore ├── README.md ├── docs └── Doxyfile ├── old ├── Data.Bool.php ├── Data.Collection.php ├── Data.Contract.IBool.php ├── Data.Contract.ICollection.php ├── Data.Contract.IEither.php ├── Data.Contract.IFunc.php ├── Data.Contract.IMaybe.php ├── Data.Contract.INum.php ├── Data.Contract.IStr.php ├── Data.Contract.ITuple.php ├── Data.Either.Left.php ├── Data.Either.Right.php ├── Data.Either.php ├── Data.Error.php ├── Data.File.php ├── Data.Func.php ├── Data.List.php ├── Data.Match.php ├── Data.Match.php.old ├── Data.Maybe.Just.php ├── Data.Maybe.Nothing.php ├── Data.Maybe.php ├── Data.Null.php ├── Data.Num.Contract.IFloat.php ├── Data.Num.Contract.IInt.php ├── Data.Num.Float.php ├── Data.Num.Int.php ├── Data.Num.php ├── Data.Object.php ├── Data.Str.php ├── Data.Test.hs ├── Data.Tuple.php ├── Data.Types.php ├── Data.Undefined.php ├── Data.Void.php ├── TODO.ml ├── contributors.hs ├── docgen │ └── imperactiveTests.php ├── extras │ ├── Constants.php │ └── Shortcuts.php ├── libs │ ├── Apache.class.php │ └── PHP.class.php ├── modules │ ├── Memoize.class.php │ └── Monad.class.php ├── patternMatching │ ├── Assertion.php │ ├── Lexer.php │ ├── TestLexer.php │ ├── Token.php │ ├── TokenBehavior.php │ └── Tokenizer.php ├── slap.log ├── typeclasses │ ├── Enum.php │ ├── Eq.interface.php │ ├── Eq.php │ ├── Ord.interface.php │ ├── Ord.php │ └── Show.php └── typing │ └── TypeInference.class.php ├── src ├── Core │ └── TypeInference.php └── DataType │ ├── Bool.php │ ├── Fun.php │ ├── Maybe.Just.php │ ├── Maybe.Nothing.php │ ├── Maybe.php │ └── Type.php └── tests ├── Main.php ├── TestBool.php └── TestFun.php /.gitignore: -------------------------------------------------------------------------------- 1 | ~* 2 | *~ 3 | #*# 4 | *.tmp.* 5 | tmp/* -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Rawr 2 | A word that means "I Love You" in dinosaur. 3 | 4 | ### What is included? 5 | 6 | ``` 7 | rawr/ 8 | ├── src/ 9 | | ├── Core/ 10 | | | └── TypeInference.php [100%] 11 | | └── DataType/ 12 | | ├── Bool.php [100%] 13 | | ├── Collection.php 14 | | ├── Either.Left.php 15 | | ├── Either.Right.php 16 | | ├── Either.php 17 | | ├── File.php 18 | | ├── Fun.php [100%] 19 | | ├── IO.php 20 | | ├── Match.php 21 | | ├── Maybe.Just.php 22 | | ├── Maybe.Nothing.php 23 | | ├── Maybe.php 24 | | ├── Num.Float.php 25 | | ├── Num.Int.php 26 | | ├── Num.php 27 | | ├── Object.php 28 | | ├── String.php 29 | | ├── Tuple.php 30 | | └── Type.php 31 | ├── tests/ 32 | ├── LICENSE 33 | └── composer.json 34 | ``` 35 | 36 | 37 | ## Do you need a README? 38 | 39 | All info can be found here: [https://haskellcamargo.github.io/rawr], ok!? 40 | -------------------------------------------------------------------------------- /old/Data.Bool.php: -------------------------------------------------------------------------------- 1 | Marcelo Camargo 3 | # @contributors => [] 4 | # @creation_date => Unkown 5 | # @last_changed => 2015-02-24 6 | # @package => Data.Bool 7 | 8 | # Copyright (c) 2014 Marcelo Camargo 9 | # 10 | # Permission is hereby granted, free of charge, to any person 11 | # obtaining a copy of this software and associated documentation files 12 | # (the "Software"), to deal in the Software without restriction, 13 | # including without limitation the rights to use, copy, modify, merge, 14 | # publish, distribute, sublicense, and/or sell copies of the Software, 15 | # and to permit persons to whom the Software is furnished to do so, 16 | # subject to the following conditions: 17 | # 18 | # The above copyright notice and this permission notice shall be 19 | # included in all copies or substantial of portions the Software. 20 | # 21 | # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 22 | # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 23 | # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 24 | # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE 25 | # LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 26 | # OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 27 | # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 28 | 29 | namespace Data; 30 | 31 | require_once 'Data.Contract.IBool.php'; 32 | use \Data\Contract\IBool as IBool; 33 | 34 | # A `Bool` type can hold two primitive values: `True` and `False`. When 35 | # starting designing Rawr, we decided to use `True` and `False` as 36 | # constructors, in a Haskell-like way, but we would have great troubles with performance and capabilities, as much as simple logic-values would be an object, an instance of a class. 37 | class Bool extends DataTypes implements IBool { 38 | function __construct($val) { # :: a -> Bool 39 | unset($this->memoize); 40 | $this->value = (bool) $val; 41 | } 42 | 43 | # Returns `Bool (True)` if both the value of the object and the value of the 44 | # received expression are true. Otherwise, false. 45 | function _and(Bool &$b) { # :: (Bool, Bool) -> Bool 46 | return new Bool($this->value && $b()); 47 | } 48 | 49 | # Returns `Bool (True)` if any of the values, of the object, or of the 50 | # received expression, are true. Otherwise, false. 51 | function _or(Bool &$b) { # :: (Bool, Bool) -> Bool 52 | return new Bool($this->value || $b()); 53 | } 54 | 55 | # Comparison of the difference of two objects or values of *same* type. 56 | function diff(Bool &$b) { # :: (Bool, Bool) -> Bool 57 | return new Bool($this->value !== $b()); 58 | } 59 | 60 | # Comparison of the equality of two objects or values of *same* type. 61 | function eq(Bool &$b) { # :: (Bool, Bool) -> Bool 62 | return new Bool($this->value === $b()); 63 | } 64 | 65 | # Returns if the value of this object is greater or equal to the received 66 | # value. 67 | function greaterOrEq(Bool &$b) { # :: (Bool, Bool) -> Bool 68 | return new Bool($this->value >= $b()); 69 | } 70 | 71 | # Returns if the value of this object is greater than the received value. 72 | function greaterThan(Bool &$b) { # :: (Bool, Bool) -> Bool 73 | return new Bool($this->value > $b()); 74 | } 75 | 76 | # The closure passed as parameter is called if the value of this object is 77 | # `Bool (True)`. After, it returns the value by itself to allow 78 | # method-chaining. 79 | function ifTrue(Func &$f) { # :: (Bool, Func) -> Bool 80 | if ($this->value === true) # Strict-comparison. 81 | $f(); 82 | return new Bool($this->value); 83 | } 84 | 85 | # The closure passed as parameter is called if the value of this object is 86 | # `Bool (False)`. After, it returns the value by itself to allow 87 | # method-chaining. 88 | function ifFalse(Func &$f) { # :: (Bool, Func) -> Bool 89 | if ($this->value === false) 90 | $clos(); 91 | return new Bool($this->value); 92 | } 93 | 94 | # Returns if the value of this object is lesser or equal to the received 95 | # value. 96 | function lesserOrEq(Bool &$b) { # :: (Bool, Bool) -> Bool 97 | return new Bool($this->value <= $b()); 98 | } 99 | 100 | # Returns if the value of this object is lesser than the received value. 101 | function lesserThan(Bool &$b) { # :: (Bool, Bool) -> Bool 102 | return new Bool($this->value < $b()); 103 | } 104 | 105 | # Reverses the value of a boolean object. 106 | function not() { # :: Bool -> Bool 107 | return new Bool(!$this->value); 108 | } 109 | 110 | # Alias to `ifFalse`. 111 | function otherwise(Func &$clos) { # :: (Bool, Func) -> Bool 112 | return $this->ifFalse($clos); 113 | } 114 | 115 | # Equivalent to `ifTrue` and `ifFalse`. 116 | function thenElse(Func &$t, Func &$e) { # :: (Bool, Func, Func) -> Bool 117 | if ($this->value === true) 118 | $t(); 119 | else 120 | $e(); 121 | return new Bool($this->value); 122 | } 123 | } 124 | -------------------------------------------------------------------------------- /old/Data.Collection.php: -------------------------------------------------------------------------------- 1 | 3 | # 4 | # Permission is hereby granted, free of charge, to any person 5 | # obtaining a copy of this software and associated documentation files 6 | # (the "Software"), to deal in the Software without restriction, 7 | # including without limitation the rights to use, copy, modify, merge, 8 | # publish, distribute, sublicense, and/or sell copies of the Software, 9 | # and to permit persons to whom the Software is furnished to do so, 10 | # subject to the following conditions: 11 | # 12 | # The above copyright notice and this permission notice shall be 13 | # included in all copies or substantial of portions the Software. 14 | # 15 | # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 16 | # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 17 | # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 18 | # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE 19 | # LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 20 | # OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 21 | # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 22 | 23 | namespace Data; 24 | use \Exception; 25 | 26 | require_once 'Data.Contract.ICollection.php'; 27 | 28 | # Collections of all types. 29 | class Collection extends DataTypes implements Contract\ICollection { 30 | private $type = "[]" 31 | , $position = 0; 32 | 33 | public function __construct() { # :: [a] -> Collection 34 | # Ensure type of all the elements in the list. 35 | # Collections are unityped. 36 | 37 | # Bindings to useful variables 38 | 39 | # Who said pattern matching doesn't exist in PHP? 40 | list ($args, $numArgs) = [func_get_args(), func_num_args()]; 41 | 42 | if (gettype($args) === "array" && $numArgs === 1) { 43 | # Received a single list in format [n, n + 1, n + 2 ...] 44 | if (($list = $args[0]) === []) # Avoid use of `empty` function here 45 | return; 46 | 47 | $let['typeConstraint'] = function ($predicate) use ($list) { # :: Closure -> (Throws TypeException) 48 | $this->type = $predicate($list[0]); 49 | # Pass by each element of the list checking if its type matches 50 | # with the type of the head of the list. 51 | foreach ($list as $item): 52 | if ($predicate($item) !== $this->type) { # Type constraint 53 | $let['t'] = is_object($item) ? get_class($item) : gettype($item); 54 | throw new Exception("Type constraint: Expecting `it` to be of type {$this->type}. Instead got {$let['t']}."); 55 | } 56 | endforeach; 57 | }; 58 | 59 | if (is_object($list[0])) 60 | $let['typeConstraint']('get_class'); 61 | else 62 | $let['typeConstraint']('gettype'); 63 | 64 | $this->value = $list; 65 | } else { 66 | # String parsing for list generation. 67 | if ($numArgs === 3 && $args[1] === '...') { 68 | $let['startAt'] = TypeInference :: to_primitive($args[0]); 69 | $let['endAt'] = TypeInference :: to_primitive($args[2]); 70 | if (gettype($let['startAt']) !== gettype($let['endAt'])) 71 | throw new Exception("List range requires start value and end value to be of the same type."); 72 | 73 | $this->type = gettype($let['startAt']); 74 | $this->value = range($let['startAt'], $let['endAt']); 75 | } else if ($numArgs === 4 && $args[2] === '...') { 76 | $let['startAt'] = TypeInference :: to_primitive($args[0]); 77 | $let['jmpVal'] = TypeInference :: to_primitive($args[1]); 78 | $let['endAt'] = TypeInference :: to_primitive($args[3]); 79 | 80 | # Type constraint 81 | if (gettype($let['startAt']) !== gettype($let['endAt']) 82 | || gettype($let['startAt']) !== gettype($let['jmpVal'])) 83 | throw new Exception("List range requires start value and end value to be of the same type."); 84 | 85 | # Not working with characters. 86 | # Range by. 87 | $let['acc'] = array(); # Accumulator 88 | for ($i = $let['startAt']; $i <= $let['endAt']; $i += ($let['jmpVal'] - $let['startAt'])) 89 | array_push($let['acc'], $i); 90 | 91 | $this->type = gettype($let['startAt']); 92 | $this->value = $let['acc']; 93 | } 94 | } 95 | } 96 | 97 | # Returns the callable part of an object 98 | private function closure($lambda) { # :: Func -> Closure 99 | if (get_class($lambda) === "Closure") 100 | return $lambda; 101 | else if (get_class($lambda) === "Data\Func") 102 | return $lambda->value(); 103 | else 104 | throw new Exception("Expecting Func or Closure."); 105 | } 106 | 107 | public function each($lambda) { # :: (Collection, Func) -> Collection 108 | $let['currying'] = $this->closure($lambda); 109 | 110 | foreach ($this->value as $item) 111 | $let['currying']($item); 112 | 113 | return new Collection($this->value); 114 | } 115 | 116 | # Filters a list according to the given predicate 117 | public function filter($lambda) { # :: (Collection, Func) -> Collection 118 | $let['acc'] = array(); 119 | $let['currying'] = $this->closure($lambda); 120 | 121 | foreach ($this->value as $item): 122 | $let['bool'] = TypeInference :: is_true($let['currying']($item)); 123 | 124 | if ($let['bool']) 125 | array_push($let['acc'], $item); 126 | 127 | endforeach; 128 | 129 | return new Collection($let['acc']); 130 | } 131 | 132 | public function intersperse($item) { 133 | $let['acc'] = array(); 134 | $let['add_pair_acc'] = function ($x, $y) use (&$let) { 135 | array_push($let['acc'], $x); 136 | array_push($let['acc'], $y); 137 | }; 138 | 139 | foreach ($this->value as $t) 140 | $let['add_pair_acc']($t, $item); 141 | 142 | return new Collection($let['acc']); 143 | } 144 | 145 | # Mapping over a list. 146 | public function map($lambda) { # :: (Collection, Func) -> Collection 147 | $let = [ 'acc' => array() 148 | , 'currying' => $this->closure($lambda)]; 149 | 150 | foreach ($this->value as $item) 151 | array_push($let['acc'], $let['currying']($item)); 152 | 153 | return new Collection($let['acc']); 154 | } 155 | 156 | # Casts the values of the list to object typed values 157 | public function of($type) { # :: Str -> Collection 158 | if ($this->type == "[]") { 159 | $this->type = str_replace(".", "\\", $type); 160 | return $this; 161 | } 162 | 163 | $let['class'] = str_replace(".", "\\", $type); 164 | 165 | if (!class_exists($let['class'])) 166 | throw new Exception("Type `{$type}` not found."); 167 | 168 | $acc = array(); 169 | for ($i = 0; $i < count($this->value); $i++) 170 | $acc[$i] = new $let['class']($this->value[$i]); 171 | return new Collection($acc); 172 | } 173 | 174 | public function reject($lambda) { # :: (Collection, Func) -> Collection 175 | $let['acc'] = array(); 176 | $let['currying'] = $this->closure($lambda); 177 | 178 | foreach ($this->value as $item): 179 | $let['bool'] = TypeInference :: is_true($let['currying']($item)); 180 | 181 | if (!$let['bool']) 182 | array_push($let['acc'], $item); 183 | 184 | endforeach; 185 | 186 | return new Collection($let['acc']); 187 | } 188 | 189 | # This automagically implements operator overloading of [] and makes this 190 | # object iterable and countable. It acts really like an Array, but it is 191 | # an object. 192 | 193 | # (Countable a) => a -> Int 194 | public function count() { 195 | return new \Data\Num\Int(sizeof($this->value)); 196 | } 197 | } -------------------------------------------------------------------------------- /old/Data.Contract.IBool.php: -------------------------------------------------------------------------------- 1 | 3 | # 4 | # Permission is hereby granted, free of charge, to any person 5 | # obtaining a copy of this software and associated documentation files 6 | # (the "Software"), to deal in the Software without restriction, 7 | # including without limitation the rights to use, copy, modify, merge, 8 | # publish, distribute, sublicense, and/or sell copies of the Software, 9 | # and to permit persons to whom the Software is furnished to do so, 10 | # subject to the following conditions: 11 | # 12 | # The above copyright notice and this permission notice shall be 13 | # included in all copies or substantial of portions the Software. 14 | # 15 | # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 16 | # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 17 | # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 18 | # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE 19 | # LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 20 | # OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 21 | # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 22 | 23 | namespace Data\Contract; 24 | use \Data\Bool as Bool; 25 | use \Data\Func as Func; 26 | 27 | interface IBool { 28 | # | METHOD NAME | METHOD ARGUMENTS | TYPE SIGNATURE 29 | function __construct ($val); # :: a -> Bool 30 | function _and (Bool &$b); # :: (Bool, Bool) -> Bool 31 | function _or (Bool &$b); # :: (Bool, Bool) -> Bool 32 | function diff (Bool &$b); # :: (Bool, Bool) -> Bool 33 | function eq (Bool &$b); # :: (Bool, Bool) -> Bool 34 | function greaterOrEq (Bool &$b); # :: (Bool, Bool) -> Bool 35 | function greaterThan (Bool &$b); # :: (Bool, Bool) -> Bool 36 | function ifTrue (Func &$f); # :: (Bool, Func) -> Bool 37 | function ifFalse (Func &$f); # :: (Bool, Func) -> Bool 38 | function lesserThan (Bool &$b); # :: (Bool, Bool) -> Bool 39 | function not (); # :: Bool -> Bool 40 | function otherwise (Func &$f); # :: (Bool, Func) -> Bool 41 | function thenElse (Func &$t, Func &$e); # :: (Bool, Func, Func) -> Bool 42 | } 43 | -------------------------------------------------------------------------------- /old/Data.Contract.ICollection.php: -------------------------------------------------------------------------------- 1 | 3 | # 4 | # Permission is hereby granted, free of charge, to any person 5 | # obtaining a copy of this software and associated documentation files 6 | # (the "Software"), to deal in the Software without restriction, 7 | # including without limitation the rights to use, copy, modify, merge, 8 | # publish, distribute, sublicense, and/or sell copies of the Software, 9 | # and to permit persons to whom the Software is furnished to do so, 10 | # subject to the following conditions: 11 | # 12 | # The above copyright notice and this permission notice shall be 13 | # included in all copies or substantial of portions the Software. 14 | # 15 | # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 16 | # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 17 | # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 18 | # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE 19 | # LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 20 | # OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 21 | # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 22 | 23 | namespace Data\Contract; 24 | use \ArrayAccess; 25 | use \Countable; 26 | use \Iterator; 27 | 28 | # /tests/exe.php => Where is the prototype of this. 29 | 30 | interface ICollection extends ArrayAccess, Countable, Iterator { 31 | public function __construct(); # :: [a] -> Collection 32 | public function each($lambda); # :: (Colllection, Func) -> Collection 33 | public function filter($lambda); # :: (Collection, Func) -> Collection 34 | public function intersperse($item); # :: (Collection, a) -> Collection 35 | public function map($lambda); # :: (Collection, Func) -> Collection 36 | public function of($type); # :: Str -> Collection 37 | public function reject($lambda); # :: (Collection, Func) -> Collection 38 | } -------------------------------------------------------------------------------- /old/Data.Contract.IEither.php: -------------------------------------------------------------------------------- 1 | 3 | # 4 | # Permission is hereby granted, free of charge, to any person 5 | # obtaining a copy of this software and associated documentation files 6 | # (the "Software"), to deal in the Software without restriction, 7 | # including without limitation the rights to use, copy, modify, merge, 8 | # publish, distribute, sublicense, and/or sell copies of the Software, 9 | # and to permit persons to whom the Software is furnished to do so, 10 | # subject to the following conditions: 11 | # 12 | # The above copyright notice and this permission notice shall be 13 | # included in all copies or substantial of portions the Software. 14 | # 15 | # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 16 | # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 17 | # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 18 | # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE 19 | # LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 20 | # OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 21 | # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 22 | 23 | namespace Data\Contract\Either; 24 | 25 | interface IEither { 26 | function either($f, $g); # :: (Either a, (a -> c), (b -> c)) -> c 27 | function isLeft(); # :: Either a -> Bool 28 | function isRight(); # :: Either b -> Bool 29 | } -------------------------------------------------------------------------------- /old/Data.Contract.IFunc.php: -------------------------------------------------------------------------------- 1 | 3 | # 4 | # Permission is hereby granted, free of charge, to any person 5 | # obtaining a copy of this software and associated documentation files 6 | # (the "Software"), to deal in the Software without restriction, 7 | # including without limitation the rights to use, copy, modify, merge, 8 | # publish, distribute, sublicense, and/or sell copies of the Software, 9 | # and to permit persons to whom the Software is furnished to do so, 10 | # subject to the following conditions: 11 | # 12 | # The above copyright notice and this permission notice shall be 13 | # included in all copies or substantial of portions the Software. 14 | # 15 | # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 16 | # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 17 | # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 18 | # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE 19 | # LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 20 | # OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 21 | # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 22 | 23 | namespace Data\Contract; 24 | 25 | interface IFunc { 26 | public function __construct($func); # :: a -> a 27 | public function ⃝($func); # (Func, Func) -> Func 28 | public function closScopeClass(); # :: Func -> ReflectionClass 29 | public function closThis(); # :: Func -> Object 30 | public function docComment(); # :: Func -> String 31 | public function endLine(); # :: Func -> Int 32 | public function export($ret = false); # :: (Func, Maybe Bool) -> String 33 | public function ext(); # :: Func -> ReflectionExtension 34 | public function extName(); # :: Func -> String 35 | public function fileName(); # :: Func -> String 36 | public function clos(); # :: Func -> Closure 37 | public function inNs(); # :: Func -> Bool 38 | public function invoke(); # :: Maybe Dynamic -> Maybe Dynamic 39 | public function isClos(); # :: Func -> Bool 40 | public function isDepr(); # :: Func -> Bool 41 | public function isDisabled(); # :: Func -> Bool 42 | public function isGen(); # :: Func -> Bool 43 | public function isInternal(); # :: Func -> Bool 44 | public function isUserDef(); # :: Func -> Bool 45 | public function isVariadic(); # :: Func -> Bool 46 | public function name(); # :: Func -> String 47 | public function ns(); # :: Func -> String 48 | public function numParam(); # :: Func -> Int 49 | public function numReqParam(); # :: Func -> Int 50 | public function param(); # :: Func -> [ReflectionParameter] 51 | public function o($func); # :: (Func, Func) -> Func 52 | public function retRef(); # :: Func -> Bool 53 | public function shortName(); # :: Func -> String 54 | public function startLine(); # :: Func -> Int 55 | public function staticVar(); # :: Func -> Collection 56 | } -------------------------------------------------------------------------------- /old/Data.Contract.IMaybe.php: -------------------------------------------------------------------------------- 1 | 3 | # 4 | # Permission is hereby granted, free of charge, to any person 5 | # obtaining a copy of this software and associated documentation files 6 | # (the "Software"), to deal in the Software without restriction, 7 | # including without limitation the rights to use, copy, modify, merge, 8 | # publish, distribute, sublicense, and/or sell copies of the Software, 9 | # and to permit persons to whom the Software is furnished to do so, 10 | # subject to the following conditions: 11 | # 12 | # The above copyright notice and this permission notice shall be 13 | # included in all copies or substantial of portions the Software. 14 | # 15 | # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 16 | # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 17 | # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 18 | # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE 19 | # LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 20 | # OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 21 | # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 22 | 23 | namespace Data\Contract\Maybe; 24 | 25 | interface IMaybe { 26 | function bind($fn); # :: (Maybe a, (a -> b)) -> b 27 | function fromJust(); # :: Maybe a -> a 28 | function fromMaybe($def); # :: (Maybe a, a) -> a 29 | function isJust(); # :: Maybe a -> Bool 30 | function isNothing(); # :: Maybe a -> Bool 31 | function maybe($def, $fn); # :: (Maybe a, b, (a -> b)) -> b 32 | function toList(); # :: Maybe a -> [a] 33 | } 34 | -------------------------------------------------------------------------------- /old/Data.Contract.INum.php: -------------------------------------------------------------------------------- 1 | 3 | # 4 | # Permission is hereby granted, free of charge, to any person 5 | # obtaining a copy of this software and associated documentation files 6 | # (the "Software"), to deal in the Software without restriction, 7 | # including without limitation the rights to use, copy, modify, merge, 8 | # publish, distribute, sublicense, and/or sell copies of the Software, 9 | # and to permit persons to whom the Software is furnished to do so, 10 | # subject to the following conditions: 11 | # 12 | # The above copyright notice and this permission notice shall be 13 | # included in all copies or substantial of portions the Software. 14 | # 15 | # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 16 | # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 17 | # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 18 | # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE 19 | # LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 20 | # OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 21 | # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 22 | 23 | namespace Data\Contract; 24 | use \Data\Num; 25 | use \Data\Num\Int; 26 | use \Data\Num\Float; 27 | use \TypeClass\Eq; 28 | 29 | # In `PHP interfaces, `public` keyword is redundant because all methods 30 | # declared in it must be obligatory public. Interfaces can act like 31 | # final abstract classes in the backend, allowing type hinting, with the singular 32 | # difference that they cannot show the implementation of a method. 33 | # Well, they are a PHP workaround with basis on final abstract classes. 34 | # That's why you cannot create a final abstract class in PHP. 35 | 36 | # Yes, in PHP interfaces can extend and, in this case, derive typeclasses. 37 | interface INum { 38 | # | METHOD NAME | METHOD ARGUMENTS | TYPE SIGNATURE 39 | function abs (); # :: Num -> Num 40 | function add (Num &$n); # :: (Num, Num) -> Num 41 | function arcCos (); # :: Num -> Float 42 | function arcSin (); # :: Num -> Float 43 | function arcTan2 (Num &$n); # :: (Num, Num) -> Float 44 | function arcTan (); # :: Num -> Float 45 | function ceil (); # :: Num -> Float 46 | function cos (); # :: Num -> Float 47 | function degToRad (); # :: Num -> Float 48 | function div (Num &$n); # :: (Num, Num) -> Num 49 | function exp (); # :: Num -> Float 50 | function expm1 (); # :: Num -> Float 51 | function floor (); # :: Num -> Float 52 | function hArcCos (); # :: Num -> Float 53 | function hArcSin (); # :: Num -> Float 54 | function hArcTan (); # :: Num -> Float 55 | function hCos (); # :: Num -> Float 56 | function hSin (); # :: Num -> Float 57 | function hTan (); # :: Num -> Float 58 | function hypot (Num &$n); # :: (Num, Num) -> Float 59 | function isFinite (); # :: Num -> Bool 60 | function isInfinite (); # :: Num -> Bool 61 | function isNAN (); # :: Num -> Bool 62 | function log10 (); # :: Num -> Float 63 | function log1p (); # :: Num -> Float 64 | function log (Num &$n = Null); # :: (Num, Num) -> Float 65 | function mod (Num &$n); # :: (Num, Num) -> Float 66 | function mul (Num &$n); # :: (Num, Num) -> Num 67 | function negate (); # :: Num -> Num 68 | function pow (Num &$n); # :: (Num, Num) -> Num 69 | function radToDeg (); # :: Num -> Float 70 | function randUntil (Num &$n); # :: (Num, Num) -> Int 71 | function round (Int &$n = Null, Int &$o = Null); # :: (Num, Int, Int) -> Float 72 | function sin (); # :: Num -> Float 73 | function sqrt (); # :: Float -> Float 74 | function sub (Num &$n); # :: (Num, Num) -> Num 75 | function tan (); # :: Num -> Float 76 | } -------------------------------------------------------------------------------- /old/Data.Contract.IStr.php: -------------------------------------------------------------------------------- 1 | 3 | # 4 | # Permission is hereby granted, free of charge, to any person 5 | # obtaining a copy of this software and associated documentation files 6 | # (the "Software"), to deal in the Software without restriction, 7 | # including without limitation the rights to use, copy, modify, merge, 8 | # publish, distribute, sublicense, and/or sell copies of the Software, 9 | # and to permit persons to whom the Software is furnished to do so, 10 | # subject to the following conditions: 11 | # 12 | # The above copyright notice and this permission notice shall be 13 | # included in all copies or substantial of portions the Software. 14 | # 15 | # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 16 | # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 17 | # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 18 | # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE 19 | # LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 20 | # OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 21 | # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 22 | 23 | namespace Data\Contract; 24 | 25 | interface IStr { 26 | public function __construct($val); # :: a -> Str 27 | public function add_cslashes(); 28 | public function add_slashes(); 29 | public function ascii_only(); 30 | public function b(); 31 | public function byte_size(); 32 | public function bytes(); 33 | public function capitalize(); # :: Str -> Str 34 | public function char($char); 35 | public function char_at($where); 36 | public function chars(); # :: Str -> Collection 37 | public function chomp($arg); 38 | public function chop(); 39 | public function chr(); # :: Str -> Str 40 | public function clear(); # :: Str -> Str 41 | public function codePoints(); # :: Str -> Collection 42 | public function cmp_case(); 43 | public function concat(); # :: (Str ...) -> Str 44 | public function contains($what); 45 | public function crypt(); 46 | public function delete($pattern); 47 | public function dump(); 48 | public function toLower(); # :: Str -> Str 49 | public function each_byte($func); 50 | public function eachChar($func); # :: (Str, Func) -> Str 51 | public function eachCodePoint($func); # :: (Str, Func) -> Str 52 | public function eachLine($func); # :: (Str, Func) -> Str 53 | public function encode(); 54 | public function ends_with($what); 55 | public function get_byte($byte); 56 | public function gsub($pattern, $by); 57 | public function hex(); 58 | public function index($of); 59 | public function insert($what); 60 | public function is_empty(); 61 | public function join($arr); # :: (Str, Collection) -> Str 62 | public function l_just(); 63 | public function l_strip(); 64 | public function length(); # :: Str -> Int 65 | public function lines(); # :: Str -> Collection 66 | public function match($with); 67 | public function next(); 68 | public function oct(); 69 | public function ord(); 70 | public function ordinal_integer(); 71 | public function putStr(); # :: Str -> Str 72 | public function putStrLn(); # :: Str -> Str 73 | public function prepend(); 74 | public function r_index($i); 75 | public function r_just(); 76 | public function r_partition(); 77 | public function r_strip(); 78 | public function repeat($times); # :: (Str, Int) -> Str 79 | public function replace($pattern, $by); 80 | public function reverse(); # :: Str -> Str 81 | public function scan(); 82 | public function scrub(); 83 | public function set_byte($byte, $val); 84 | public function shuffle(); 85 | public function slice($i, $j); 86 | public function split($pattern); 87 | public function squeeze(); 88 | public function starts_with($what); 89 | public function strip(); 90 | public function succ(); 91 | public function sum(); 92 | public function swapCase(); # :: Str -> Str 93 | public function unpack(); 94 | public function toUpper(); # :: Str -> Str 95 | public function words(); # :: Str -> Collection 96 | } -------------------------------------------------------------------------------- /old/Data.Contract.ITuple.php: -------------------------------------------------------------------------------- 1 | 3 | # 4 | # Permission is hereby granted, free of charge, to any person 5 | # obtaining a copy of this software and associated documentation files 6 | # (the "Software"), to deal in the Software without restriction, 7 | # including without limitation the rights to use, copy, modify, merge, 8 | # publish, distribute, sublicense, and/or sell copies of the Software, 9 | # and to permit persons to whom the Software is furnished to do so, 10 | # subject to the following conditions: 11 | # 12 | # The above copyright notice and this permission notice shall be 13 | # included in all copies or substantial of portions the Software. 14 | # 15 | # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 16 | # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 17 | # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 18 | # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE 19 | # LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 20 | # OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 21 | # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 22 | 23 | namespace Data\Contract; 24 | use \Data\Num\Int; 25 | 26 | interface ITuple { 27 | function get(Int $index); # :: (Tuple, Num) -> Maybe a 28 | function fst(); # :: Tuple -> Maybe a 29 | function snd(); # :: Tuple -> Maybe b 30 | function showType(); # :: Tuple -> Str 31 | function swap(); # :: Tuple -> Tuple 32 | } 33 | -------------------------------------------------------------------------------- /old/Data.Either.Left.php: -------------------------------------------------------------------------------- 1 | Marcelo Camargo 3 | # @contributors => [] 4 | # @creation_date => Unkown 5 | # @last_changed => 2015-02-24 6 | # @package => Data.Either.Left 7 | 8 | # Copyright (c) 2014 Marcelo Camargo 9 | # 10 | # Permission is hereby granted, free of charge, to any person 11 | # obtaining a copy of this software and associated documentation files 12 | # (the "Software"), to deal in the Software without restriction, 13 | # including without limitation the rights to use, copy, modify, merge, 14 | # publish, distribute, sublicense, and/or sell copies of the Software, 15 | # and to permit persons to whom the Software is furnished to do so, 16 | # subject to the following conditions: 17 | # 18 | # The above copyright notice and this permission notice shall be 19 | # included in all copies or substantial of portions the Software. 20 | # 21 | # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 22 | # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 23 | # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 24 | # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE 25 | # LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 26 | # OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 27 | # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 28 | 29 | namespace Data\Either; 30 | use \Data\Contract\Either\IEither; 31 | 32 | # This class can hold an error value, an operation that has failures. 33 | class Left extends \Data\Either implements IEither { 34 | function __construct($value) { 35 | $this->value = $value; 36 | } 37 | 38 | # Case analysis for the `Either` type. If the value is `Left a`, apply the 39 | # first function to a; if it is `Right b`, apply the second function to b. 40 | function either($f, $_) { # :: (Either a b, Func, Func) -> c 41 | return $f($this->value); 42 | } 43 | 44 | # Return `Bool (True)` if the given value is a `Left`-value, `Bool (False)` 45 | # otherwise. 46 | function isLeft() { # :: Either a b -> Bool 47 | return Bool(True); 48 | } 49 | 50 | # Return `Bool (True)` if the given value is a `Right`-value, `Bool (False)` 51 | # otherwise. 52 | function isRight() { # :: Either a b -> Bool 53 | return Bool(False); 54 | } 55 | } 56 | -------------------------------------------------------------------------------- /old/Data.Either.Right.php: -------------------------------------------------------------------------------- 1 | Marcelo Camargo 3 | # @contributors => [] 4 | # @creation_date => Unkown 5 | # @last_changed => 2015-02-24 6 | # @package => Data.Either.Right 7 | 8 | # Copyright (c) 2014 Marcelo Camargo 9 | # 10 | # Permission is hereby granted, free of charge, to any person 11 | # obtaining a copy of this software and associated documentation files 12 | # (the "Software"), to deal in the Software without restriction, 13 | # including without limitation the rights to use, copy, modify, merge, 14 | # publish, distribute, sublicense, and/or sell copies of the Software, 15 | # and to permit persons to whom the Software is furnished to do so, 16 | # subject to the following conditions: 17 | # 18 | # The above copyright notice and this permission notice shall be 19 | # included in all copies or substantial of portions the Software. 20 | # 21 | # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 22 | # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 23 | # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 24 | # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE 25 | # LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 26 | # OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 27 | # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 28 | 29 | namespace Data\Either; 30 | use \Data\Contract\Either\IEither; 31 | 32 | # This carries the value if it passes in the tests and can't generate an 33 | # error. 34 | class Right extends \Data\Either implements IEither { 35 | function __construct($value) { 36 | $this->value = $value; 37 | } 38 | 39 | # Case analysis for the `Either` type. If the value is `Left a`, apply the 40 | # first function to a; if it is `Right b`, apply the second function to b. 41 | function either($_, $g) { # :: (Either a b, Func, Func) -> c 42 | return $g($this->value); 43 | } 44 | 45 | # Return `Bool (True)` if the given value is a `Left`-value, `Bool (False)` 46 | # otherwise. 47 | function isLeft() { # :: Either a b -> Bool 48 | return Bool(False); 49 | } 50 | 51 | # Return `Bool (True)` if the given value is a `Right`-value, `Bool (False)` 52 | # otherwise. 53 | function isRight() { # :: Either a b -> Bool 54 | return Bool(True); 55 | } 56 | } 57 | -------------------------------------------------------------------------------- /old/Data.Either.php: -------------------------------------------------------------------------------- 1 | Marcelo Camargo 3 | # @contributors => [] 4 | # @creation_date => Unkown 5 | # @last_changed => 2015-02-24 6 | # @package => Data.Either 7 | 8 | # Copyright (c) 2014 Marcelo Camargo 9 | # 10 | # Permission is hereby granted, free of charge, to any person 11 | # obtaining a copy of this software and associated documentation files 12 | # (the "Software"), to deal in the Software without restriction, 13 | # including without limitation the rights to use, copy, modify, merge, 14 | # publish, distribute, sublicense, and/or sell copies of the Software, 15 | # and to permit persons to whom the Software is furnished to do so, 16 | # subject to the following conditions: 17 | # 18 | # The above copyright notice and this permission notice shall be 19 | # included in all copies or substantial of portions the Software. 20 | # 21 | # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 22 | # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 23 | # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 24 | # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE 25 | # LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 26 | # OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 27 | # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 28 | 29 | namespace Data; 30 | 31 | require_once 'Data.Contract.IEither.php'; 32 | require_once 'Data.Either.Left.php'; 33 | require_once 'Data.Either.Right.php'; 34 | 35 | # The `Either` type represents values with two possibilities: a value of type 36 | # `Either a b` is either `Left a` or `Right b`. 37 | # The `Either` type is sometimes used to represent a value which is either 38 | # correct or an error; by convention, the `Left` constructor is used to hold 39 | # an error value and the `Right` constructor is used to hold a correct value 40 | # (mnemonic: "right" also means "correct"). 41 | abstract class Either extends DataTypes { 42 | # Case analysis for the `Either` type. If the value is `Left a`, apply the 43 | # first function to a; if it is `Right b`, apply the second function to b. 44 | abstract function either($f, $g); # :: (Either a b, Func, Func) -> c 45 | 46 | # Return `Bool (True)` if the given value is a `Left`-value, `Bool (False)` 47 | # otherwise. 48 | abstract function isLeft(); # :: Either a b -> Bool 49 | 50 | # Return `Bool (True)` if the given value is a `Right`-value, `Bool (False)` 51 | # otherwise. 52 | abstract function isRight(); # :: Either a b -> Bool 53 | } 54 | 55 | # This function works like a constructor for the `Either` monad. It checks 56 | # the received value and return `Data.Either.Left x` or `Data.Either.Right x`. 57 | function Either($v) { 58 | if (is_null($v) || (is_object($v) && get_class($v) === "Data\\Null")) 59 | return new \Data\Either\Left($v); 60 | return new \Data\Either\Right($v); 61 | } 62 | -------------------------------------------------------------------------------- /old/Data.Error.php: -------------------------------------------------------------------------------- 1 | Marcelo Camargo 3 | # @contributors => [] 4 | # @creation_date => Unkown 5 | # @last_changed => 2015-02-25 6 | # @package => Data.Error 7 | 8 | # Copyright (c) 2014 Haskell Camargo 9 | # 10 | # Permission is hereby granted, free of charge, to any person 11 | # obtaining a copy of this software and associated documentation files 12 | # (the "Software"), to deal in the Software without restriction, 13 | # including without limitation the rights to use, copy, modify, merge, 14 | # publish, distribute, sublicense, and/or sell copies of the Software, 15 | # and to permit persons to whom the Software is furnished to do so, 16 | # subject to the following conditions: 17 | # 18 | # The above copyright notice and this permission notice shall be 19 | # included in all copies or substantial of portions the Software. 20 | # 21 | # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 22 | # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 23 | # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 24 | # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE 25 | # LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 26 | # OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 27 | # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 28 | 29 | namespace Data; 30 | use \Exception; 31 | 32 | class Error extends DataTypes { 33 | function __construct(Str $message, Num\Int $code) { 34 | # It receives the data as primitive PHP types, as much as we'll not work 35 | # with their behaviors. 36 | $this->value = ["message" => $message 37 | , "code" => isset($code) ? $code : 0 ]; 38 | } 39 | 40 | function send() { 41 | echo "*** Runtime error:\n" 42 | . " code => {$this->value["code"]}\n" 43 | . " message => {$this->value["message"]}\n"; 44 | } 45 | 46 | function after(Func $fn) { 47 | $fn(Tuple($this->value["code"], $this->value["message"])); 48 | } 49 | } 50 | -------------------------------------------------------------------------------- /old/Data.File.php: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/haskellcamargo/rawr/5f49fc8bdb0096db4d09b77d6260be6f7829ef89/old/Data.File.php -------------------------------------------------------------------------------- /old/Data.Func.php: -------------------------------------------------------------------------------- 1 | 3 | # 4 | # Permission is hereby granted, free of charge, to any person 5 | # obtaining a copy of this software and associated documentation files 6 | # (the "Software"), to deal in the Software without restriction, 7 | # including without limitation the rights to use, copy, modify, merge, 8 | # publish, distribute, sublicense, and/or sell copies of the Software, 9 | # and to permit persons to whom the Software is furnished to do so, 10 | # subject to the following conditions: 11 | # 12 | # The above copyright notice and this permission notice shall be 13 | # included in all copies or substantial of portions the Software. 14 | # 15 | # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 16 | # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 17 | # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 18 | # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE 19 | # LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 20 | # OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 21 | # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 22 | 23 | namespace Data; 24 | use \ReflectionFunction; 25 | use \Data\Contract\IFunc as IFunc; 26 | use \TypeClass\Eq as Eq; 27 | 28 | require_once 'Data.Contract.IFunc.php'; 29 | 30 | class Func extends DataTypes implements IFunc { 31 | public $reflection; 32 | 33 | public function __construct($func) { # :: a -> a 34 | if (!is_callable($func) && !is_string($func)) 35 | throw new Exception; # Not a closure. 36 | else { 37 | parent :: __construct(); 38 | $this->value = $func; 39 | $this->reflection = new ReflectionFunction($func); 40 | } 41 | } 42 | 43 | public function __invoke() { # :: a -> a 44 | if (func_get_args() < $this->reflection->getNumberOfRequiredParameters()) 45 | throw new Exception; # Required arguments. 46 | 47 | if (count($args = func_get_args()) > 0) 48 | return TypeInference :: infer(call_user_func_array($this->value, $args)); 49 | else 50 | return TypeInference :: infer(call_user_func($this->value())); 51 | } 52 | 53 | # Function composition. 54 | public function ⃝($func) { # :: (Func, Func) -> Func 55 | # Math definition: (f ⃝ g)(x) = f(g(x)) where 56 | $let = [ 57 | "f" => $this -> value 58 | , "g" => \Data\TypeInference :: toPrimitive($func) 59 | ]; 60 | return new Func(function ($x) use ($let) { 61 | return $let['f']($let['g']($x)); 62 | }); 63 | } 64 | 65 | public function closScopeClass() { # :: Func -> ReflectionClass 66 | return $this->reflection->getClosureScopeClass(); 67 | } 68 | 69 | public function closThis() { # :: Func -> Object 70 | return $this->reflection->getClosureThis(); 71 | } 72 | 73 | public function docComment() { # :: Func -> Str 74 | return ($temp = $this->reflection->getDocComment()) === false? 75 | new Bool(false) 76 | : new Str($temp); 77 | } 78 | 79 | public function endLine() { # :: Func -> Int 80 | return new Int($this->reflection->getEndLine()); 81 | } 82 | 83 | public function export($ret = false) { # :: (Str, Maybe Bool) -> Str 84 | return new Str(ReflectionFunction :: export($this->value, $ret)); 85 | } 86 | 87 | public function ext() { # :: Func -> ReflectionExtension 88 | return $this->reflection->getExtension(); 89 | } 90 | 91 | public function extName() { # :: Func -> Str 92 | return ($temp = $this->reflection->getExtensionName()) === false? 93 | new Bool(false) 94 | : new Str($temp); 95 | } 96 | 97 | public function fileName() { # :: Func -> Str 98 | return new Str($this->reflection->getFileName()); 99 | } 100 | 101 | public function clos() { # :: Func -> Closure 102 | return $this->reflection->getClosure(); 103 | } 104 | 105 | public function inNs() { # :: Func -> Bool 106 | return new Bool($this->reflection->inNamespace()); 107 | } 108 | 109 | # Own implementation for a 100% optimization in velocity. 110 | public function invoke() { # :: Maybe Dynamic -> Maybe Dynamic 111 | if (func_get_args() < $this->reflection->getNumberOfRequiredParameters()) 112 | throw new Exception; # Required arguments. 113 | 114 | if (count($args = func_get_args()) > 0) 115 | return TypeInference :: infer(call_user_func_array($this->value, $args)); 116 | else 117 | return TypeInference :: infer(call_user_func($this->value())); 118 | } 119 | 120 | public function isClos() { # :: Func -> Bool 121 | return new Bool($this->reflection->isClosure()); 122 | } 123 | 124 | public function isDepr() { # :: Func -> Bool 125 | return new Bool($this->reflection->isDeprecated()); 126 | } 127 | 128 | public function isDisabled() { # :: Func -> Bool 129 | return new Bool($this->reflection->isDisabled()); 130 | } 131 | 132 | public function isGen() { # :: Func -> Bool 133 | return new Bool($this->reflection->isGenerator()); 134 | } 135 | 136 | public function isInternal() { # :: Func -> Bool 137 | return new Bool($this->reflection->isInternal()); 138 | } 139 | 140 | public function isUserDef() { # :: Func -> Bool 141 | return new Bool($this->reflection->isUserDefined()); 142 | } 143 | 144 | public function isVariadic() { # :: Func -> Bool 145 | return new Bool($this->reflection->isVariadic()); 146 | } 147 | 148 | public function name() { # :: Func -> Str 149 | return new Str($this->reflection->getName()); 150 | } 151 | 152 | public function ns() { # :: Func -> Str 153 | return new Str($this->reflection->getNamespaceName()); 154 | } 155 | 156 | public function numParam() { # :: Func -> Int 157 | return new Int($this->reflection->getNumberOfParameters()); 158 | } 159 | 160 | public function numReqParam() { # :: Func -> Int 161 | return new Int($this->reflection->getNumberOfRequiredParameters()); 162 | } 163 | 164 | # Alias to function composition. 165 | public function o($func) { # :: (Func, Func) -> Func 166 | # (f ⃝ g)(x) = f(g(x)) 167 | return $this->⃝($func); 168 | } 169 | 170 | public function param() { # :: Func -> [ReflectionParameter] 171 | return new Collection($this->reflection->getParameters()); 172 | } 173 | 174 | public function retRef() { # :: Func -> Bool 175 | return new Bool($this->reflection->returnsReference()); 176 | } 177 | 178 | public function shortName() { # :: Func -> Str 179 | return new Str($this->reflection->getShortName()); 180 | } 181 | 182 | public function startLine() { # :: Func -> Int 183 | return new Int($this->reflection->getStartLine()); 184 | } 185 | 186 | public function staticVar() { # :: Func -> Collection 187 | return new Collection($this->reflection->getStaticVariables()); 188 | } 189 | } -------------------------------------------------------------------------------- /old/Data.List.php: -------------------------------------------------------------------------------- 1 | 3 | # 4 | # Permission is hereby granted, free of charge, to any person 5 | # obtaining a copy of this software and associated documentation files 6 | # (the "Software"), to deal in the Software without restriction, 7 | # including without limitation the rights to use, copy, modify, merge, 8 | # publish, distribute, sublicense, and/or sell copies of the Software, 9 | # and to permit persons to whom the Software is furnished to do so, 10 | # subject to the following conditions: 11 | # 12 | # The above copyright notice and this permission notice shall be 13 | # included in all copies or substantial of portions the Software. 14 | # 15 | # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 16 | # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 17 | # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 18 | # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE 19 | # LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 20 | # OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 21 | # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 22 | 23 | namespace Data; 24 | 25 | # Collections of all types. 26 | class Collection extends DataTypes { 27 | public function __construct($arr) { 28 | $this->value = $arr; 29 | return $this; 30 | } 31 | 32 | # Returns a new list which contains only the truthy values of the inputted list. 33 | # [a] → [a] 34 | public function compact() { 35 | $arr = Array(); 36 | foreach ($this->value as $atom) { 37 | if ($atom) array_push($arr, $atom); 38 | } 39 | $this->value = $arr; 40 | return $this; 41 | } 42 | 43 | # Applies a function to each item in the list and returns the original list. 44 | # (a → Undefined) → [a] →[a] 45 | public function each($clos) { 46 | foreach ($this->value as $atom) 47 | $clos($atom); 48 | return $this; 49 | } 50 | 51 | # Applies a function to each string in the list and returns the original list. 52 | # (a → String) → [a] →[a] 53 | public function each_s($clos) { 54 | foreach ($this->value as $atom) 55 | $clos(new String($atom)); 56 | return $this; 57 | } 58 | 59 | # Returns a new list composed of the items which pass the supplied function's 60 | # test. 61 | # (a → Boolean) → [a] → [a] 62 | public function filter($clos) { 63 | $arr = Array(); 64 | foreach ($this->value as $atom) 65 | if ($clos($atom)) 66 | array_push($arr, $atom); 67 | $this->value = $arr; 68 | return $this; 69 | } 70 | 71 | # A function which does nothing: it simply returns its single argument. 72 | # Useful as a placeholder. 73 | # a → a 74 | public function id() { 75 | return $this; 76 | } 77 | 78 | # Returns an element by index. 79 | # [a] → [a] 80 | public function index_arr($i) { 81 | $res = (array) $this->value; 82 | $to_primitive = Array(); 83 | foreach ($res as $result) 84 | array_push($to_primitive, $result); 85 | $this->value = new Collection($to_primitive[0][$i]); 86 | return $this; 87 | } 88 | 89 | # Returns an element by index. 90 | # [a] → [a] 91 | public function on($i) { 92 | $res = (array) $this->value; 93 | $this->value = $res[$i]; 94 | return $this; 95 | } 96 | 97 | # Takes a string (type name) and a value, and returns if the value is of 98 | # that type. Uses PHP's typeof operator. 99 | # String → a → Boolean 100 | public function is_type($type) { 101 | return new Boolean(typeof($this->value) == $type); 102 | } 103 | 104 | # Applies a function to each item in the list, and produces a new list with the 105 | # results. The length of the result is the same length as the input. 106 | # (a → b) → [a] → [b] 107 | public function map($clos) { 108 | $arr = Array(); 109 | foreach ($this->value as $atom) 110 | array_push($arr, $clos($atom)); 111 | $this->value = $arr; 112 | return $this; 113 | } 114 | 115 | # Returns the mean of a collection 116 | # 117 | 118 | # Merges two collections in one 119 | # [a] → [a] 120 | public function merge($collection) { 121 | $this->value = array_merge($this->value, $collection); 122 | return $this; 123 | } 124 | 125 | # Gives the product of a numeric collection 126 | # [a] → Maybe a 127 | public function product() { 128 | // TODO: Memoize it. 129 | $arr = (array) $this->value; 130 | $product = $arr[0]; 131 | foreach ($this->value as $entry) { 132 | $product *= $entry; 133 | } 134 | return new Integer($product); 135 | } 136 | 137 | # Equivalent to [(filter f, xs), (reject f, xs)], but more efficient, 138 | # using only one loop. 139 | # (a → Boolean) → [a] → [[a], [a]] 140 | public function partition($clos) { 141 | $filter = Array(); 142 | $reject = Array(); 143 | foreach ($this->value as $atom) 144 | if ($clos($atom)) 145 | array_push($filter, $atom); 146 | else array_push($reject, $atom); 147 | $this->value = new Collection(Array($filter, $reject)); 148 | return $this; 149 | } 150 | 151 | # Removes the last element of the collection 152 | # [a] → [a] 153 | public function pop() { 154 | array_pop($this->value); 155 | return $this; 156 | } 157 | 158 | # Pushes an element to the collection 159 | # [a] → [a] 160 | public function push($atom) { 161 | array_push($this->value, $atom); 162 | return $this; 163 | } 164 | 165 | # Like filter, but the new list is composed of all the items which fail the 166 | # function's test. 167 | # (a → Boolean) → [a] → [a] 168 | public function reject($clos) { 169 | $arr = Array(); 170 | foreach ($this->value as $atom) 171 | if (!$clos($atom)) 172 | array_push($arr, $atom); 173 | $this->value = $arr; 174 | return $this; 175 | } 176 | 177 | # Reverses a collection 178 | # [a] → [a] 179 | public function reverse() { 180 | $this->value = array_reverse($this->value); 181 | return $this; 182 | } 183 | 184 | # Gets the sum of the items in the collection 185 | # [a] → Integer 186 | public function sum() { 187 | return new Integer(array_sum($this->value)); 188 | } 189 | } -------------------------------------------------------------------------------- /old/Data.Match.php: -------------------------------------------------------------------------------- 1 | Marcelo Camargo 3 | # @contributors => [] 4 | # @creation_date => Unkown 5 | # @last_changed => 2015-02-25 6 | # @package => Data.Match 7 | 8 | # Copyright (c) 2014 Marcelo Camargo 9 | # 10 | # Permission is hereby granted, free of charge, to any person 11 | # obtaining a copy of this software and associated documentation files 12 | # (the "Software"), to deal in the Software without restriction, 13 | # including without limitation the rights to use, copy, modify, merge, 14 | # publish, distribute, sublicense, and/or sell copies of the Software, 15 | # and to permit persons to whom the Software is furnished to do so, 16 | # subject to the following conditions: 17 | # 18 | # The above copyright notice and this permission notice shall be 19 | # included in all copies or substantial of portions the Software. 20 | # 21 | # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 22 | # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 23 | # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 24 | # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE 25 | # LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 26 | # OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 27 | # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 28 | 29 | namespace Data; 30 | 31 | class Match { 32 | private $value; 33 | 34 | function __construct($value) { 35 | $this->value = $value; 36 | } 37 | 38 | # Private methods 39 | 40 | private function matchLiteral($patternList) { 41 | # :: (Match a, Array) -> Either Error c 42 | foreach ($patternList as $pattern => $whenMatch) { 43 | if ($this->value == $pattern) 44 | return Right($whenMatch); 45 | 46 | if (isset($patternList[otherwise])) 47 | return Right($patternList[otherwise]); 48 | 49 | return Left(Error(Str("No pattern found for the given value"), Int(0))); 50 | } 51 | } 52 | 53 | private function matchObject($patternList) { 54 | # :: (Match a, Array) -> Either Error c 55 | $storedPatterns = [/* pattern => [ value :: [Str] 56 | , type :: Int 57 | , func :: Func 58 | ]*/]; 59 | foreach ($patternList as $pattern => $whenMatch) { 60 | $words = explode(' ', $pattern); 61 | 62 | $patternType = HoldsValue; 63 | 64 | /** 65 | * How is the type definition for patterns. 66 | * Tokenization of the elements 67 | * TODO: Write a small predictive top-down recursive-descent parse 68 | * for matching patterns. 69 | * 70 | * +----------------------------+-------------------------------------+ 71 | * | Exemplo | Abstract syntax tree | 72 | * |----------------------------+-------------------------------------+ 73 | * | Just x | Obj Var | 74 | * | Nothing | Obj | 75 | * | Int x | Obj Var | 76 | * | Either a b | Obj Var Var | 77 | * | Just Int x | Obj Obj Var | 78 | * | Just Int _ | Obj Obj Wildcard | 79 | * | Either (Err e) (Str x) | Obj | 80 | * | | Obj Var | 81 | * | | Obj Var | 82 | * +----------------------------+-------------------------------------+ 83 | * 84 | */ 85 | 86 | $storedPatterns[] = [$pattern => [ /* value */ $words 87 | , /* type */ $patternType 88 | , /* func */ $whenMatch 89 | ]]; 90 | } 91 | 92 | var_dump($let); 93 | } 94 | 95 | function with(array $patternList) { 96 | # :: (Match a, Array) -> Either Error c 97 | # First, we verify if we want match an object (that can have constructor) 98 | # or a primitive scalar PHP value. 99 | return is_object($this->value) ? $this->matchObject($patternList) 100 | /* otherwise */ : $this->matchLiteral($patternList); 101 | } 102 | } 103 | -------------------------------------------------------------------------------- /old/Data.Match.php.old: -------------------------------------------------------------------------------- 1 | 3 | # 4 | # Permission is hereby granted, free of charge, to any person 5 | # obtaining a copy of this software and associated documentation files 6 | # (the "Software"), to deal in the Software without restriction, 7 | # including without limitation the rights to use, copy, modify, merge, 8 | # publish, distribute, sublicense, and/or sell copies of the Software, 9 | # and to permit persons to whom the Software is furnished to do so, 10 | # subject to the following conditions: 11 | # 12 | # The above copyright notice and this permission notice shall be 13 | # included in all copies or substantial of portions the Software. 14 | # 15 | # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 16 | # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 17 | # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 18 | # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE 19 | # LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 20 | # OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 21 | # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 22 | 23 | namespace Data; 24 | 25 | class Match { 26 | private $value; 27 | 28 | function __construct($value) { 29 | $this->value = $value; 30 | } 31 | 32 | private function isIdent($item) { 33 | if (!ctype_alpha($item[0])) 34 | return false; 35 | 36 | for ($i = 1; $i < strlen($item); $i++) 37 | if (!ctype_alnum($item[$i]) && !ctype_punct($item[$i])) 38 | return false; 39 | 40 | return true; 41 | } 42 | 43 | private function validateConstrPattern($pattern) { 44 | $patternDivision = explode(" ", $pattern); 45 | if (($sizeArg = sizeof($patternDivision)) != 1 && $sizeArg != 2) 46 | throw new \Exception("Error on parsing arguments"); 47 | 48 | foreach ($patternDivision as $item) 49 | if (!$this->isIdent($item) && $item !== otherwise) 50 | throw new \Exception("Invalid pattern"); 51 | 52 | return $patternDivision; 53 | } 54 | 55 | private function verifyArguments($sizeThatShouldBe, $patternDivision, $do) { 56 | if ($sizeThatShouldBe == 1) return; 57 | 58 | if (($x = $patternDivision[1]) == ($y = (new \ReflectionFunction($do)) 59 | -> getParameters()[0] -> name)) 60 | return; 61 | 62 | throw new \Exception("Unmatching arguments: {{$x}} with {{$y}}"); 63 | } 64 | 65 | private function _withConstr($patternList) { 66 | $objectClass = DataTypes :: typeName(get_class($this->value)); 67 | 68 | foreach ($patternList as $pattern => $do) { 69 | $patternDivision = $this->validateConstrPattern($pattern); 70 | $sizeThatShouldBe = (isset($this->value->value) 71 | || $this->value->value() === null ) ? 2 : 1; 72 | 73 | if (sizeof($patternDivision) == $sizeThatShouldBe 74 | && $objectClass == $patternDivision[0]) { 75 | $this->verifyArguments($sizeThatShouldBe, $patternDivision, $do); 76 | return $do($this->value); 77 | } 78 | } 79 | 80 | if (isset($patternList[otherwise])) 81 | return $patternList[otherwise]($this->value); 82 | 83 | throw new \Exception("No matching pattern found for given value"); 84 | } 85 | 86 | function withConstr($patternList = []) { 87 | if (!is_object($this->value)) 88 | throw new \Exception("Pattern-matching with constructors just can be " 89 | . "applied for objects"); 90 | switch ($patternList) { 91 | case []: 92 | throw new \Exception("No patterns have been specified."); 93 | default: 94 | return $this->_withConstr($patternList); 95 | } 96 | } 97 | } 98 | -------------------------------------------------------------------------------- /old/Data.Maybe.Just.php: -------------------------------------------------------------------------------- 1 | Marcelo Camargo 3 | # @contributors => [] 4 | # @creation_date => Unkown 5 | # @last_changed => 2015-02-24 6 | # @package => Data.Maybe.Just 7 | 8 | # Copyright (c) 2014 Marcelo Camargo 9 | # 10 | # Permission is hereby granted, free of charge, to any person 11 | # obtaining a copy of this software and associated documentation files 12 | # (the "Software"), to deal in the Software without restriction, 13 | # including without limitation the rights to use, copy, modify, merge, 14 | # publish, distribute, sublicense, and/or sell copies of the Software, 15 | # and to permit persons to whom the Software is furnished to do so, 16 | # subject to the following conditions: 17 | # 18 | # The above copyright notice and this permission notice shall be 19 | # included in all copies or substantial of portions the Software. 20 | # 21 | # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 22 | # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 23 | # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 24 | # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE 25 | # LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 26 | # OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 27 | # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 28 | 29 | namespace Data\Maybe; 30 | use \Data\Func; 31 | use \Data\Contract\Maybe\IMaybe; 32 | 33 | # `Just` is a constructor of the `Maybe` type/monad. It must carry a value. 34 | class Just extends \Data\Maybe implements IMaybe { 35 | function __construct($value) { 36 | $this->value = $value; 37 | } 38 | 39 | # Equivalent to Haskell's `>>=` operator. Its first argument is a value in 40 | # a monadic type, its second argument is a function that maps from the 41 | # underlying type of the first argument to another monadic type, and its 42 | # results is in that other monadic type. 43 | function bind($fn) { # :: (Maybe a, Func) -> Maybe b 44 | return $fn($this->value); 45 | } 46 | 47 | # Extracts the element out of a `Just` and returns an error if its argument 48 | # is `Nothing`. 49 | function fromJust() { # :: Maybe a -> a 50 | return $this->value; 51 | } 52 | 53 | # Takes a `Maybe` value and a default value. If the `Maybe` is `Nothing`, it 54 | # returns the default values; otherwise, it returns the value contained in 55 | # the `Maybe`. 56 | function fromMaybe($_) { # :: (Maybe a, a) -> a 57 | return $this->value; 58 | } 59 | 60 | # Returns `Bool (True)` if its argument is of the form `Just _`. 61 | function isJust() { # :: Maybe a -> Bool 62 | return Bool(True); 63 | } 64 | 65 | # Returns `Bool (True)` if its arguments is of the form `Nothing`. 66 | function isNothing() { # :: Maybe a -> Bool 67 | return Bool(False); 68 | } 69 | 70 | # Takes a default value, a function and, of course, a `Maybe` value. If the 71 | # `Maybe` value is `Nothing`, the function returns the default value. 72 | # Otherwise, it applies the function to the value inside the `Just` and 73 | # returns the result. 74 | function maybe($_, $fn) { # :: (Maybe a, b, Func) -> b 75 | return $fn($this->value); 76 | } 77 | 78 | # Returns an empty list when given ``Nothing`` or a singleton list when not 79 | # given ``Nothing``. 80 | function toList() { # :: Maybe a -> Collection 81 | return Collection([$this->value]); 82 | } 83 | } 84 | -------------------------------------------------------------------------------- /old/Data.Maybe.Nothing.php: -------------------------------------------------------------------------------- 1 | Marcelo Camargo 3 | # @contributors => [] 4 | # @creation_date => Unkown 5 | # @last_changed => 2015-02-24 6 | # @package => Data.Maybe.Nothing 7 | 8 | # Copyright (c) 2014 Marcelo Camargo 9 | # 10 | # Permission is hereby granted, free of charge, to any person 11 | # obtaining a copy of this software and associated documentation files 12 | # (the "Software"), to deal in the Software without restriction, 13 | # including without limitation the rights to use, copy, modify, merge, 14 | # publish, distribute, sublicense, and/or sell copies of the Software, 15 | # and to permit persons to whom the Software is furnished to do so, 16 | # subject to the following conditions: 17 | # 18 | # The above copyright notice and this permission notice shall be 19 | # included in all copies or substantial of portions the Software. 20 | # 21 | # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 22 | # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 23 | # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 24 | # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE 25 | # LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 26 | # OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 27 | # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 28 | 29 | namespace Data\Maybe; 30 | use \Data\Func; 31 | use \Exception; 32 | use \Data\Contract\Maybe\IMaybe; 33 | 34 | # `Nothing` is a constructor of the `Maybe` type/monad. It doesn't take value. 35 | class Nothing extends \Data\Maybe implements IMaybe { 36 | 37 | # Equivalent to Haskell's `>>=` operator. Its first argument is a value in 38 | # a monadic type, its second argument is a function that maps from the 39 | # underlying type of the first argument to another monadic type, and its 40 | # results is in that other monadic type. 41 | function bind($_) { # :: (Maybe a, Func) -> Maybe b 42 | return $this; 43 | } 44 | 45 | # Extracts the element out of a `Just` and returns an error if its argument 46 | # is `Nothing`. 47 | function fromJust() { # :: Maybe a -> a 48 | throw new \Exception("Cannot cal val() nothing"); 49 | } 50 | 51 | # Takes a `Maybe` value and a default value. If the `Maybe` is `Nothing`, it 52 | # returns the default values; otherwise, it returns the value contained in 53 | # the `Maybe`. 54 | function fromMaybe($def) { # :: (Maybe a, a) -> a 55 | return $def; 56 | } 57 | 58 | # Returns `Bool (True)` if its argument is of the form `Just _`. 59 | function isJust() { # :: Maybe a -> Bool 60 | return Bool(False); 61 | } 62 | 63 | # Returns `Bool (True)` if its arguments is of the form `Nothing`. 64 | function isNothing() { # :: Maybe a -> Bool 65 | return Bool(True); 66 | } 67 | 68 | # Takes a default value, a function and, of course, a `Maybe` value. If the 69 | # `Maybe` value is `Nothing`, the function returns the default value. 70 | # Otherwise, it applies the function to the value inside the `Just` and 71 | # returns the result. 72 | function maybe($def, $_) { # :: (Maybe a, b, Func) -> b 73 | return $def; 74 | } 75 | 76 | # Returns an empty list when given ``Nothing`` or a singleton list when not 77 | # given ``Nothing``. 78 | function toList() { # :: Maybe a -> Collection 79 | return Collection([]); 80 | } 81 | } 82 | -------------------------------------------------------------------------------- /old/Data.Maybe.php: -------------------------------------------------------------------------------- 1 | Marcelo Camargo 3 | # @contributors => [] 4 | # @creation_date => Unkown 5 | # @last_changed => 2015-02-24 6 | # @package => Data.Maybe 7 | 8 | # Copyright (c) 2014 Marcelo Camargo 9 | # 10 | # Permission is hereby granted, free of charge, to any person 11 | # obtaining a copy of this software and associated documentation files 12 | # (the "Software"), to deal in the Software without restriction, 13 | # including without limitation the rights to use, copy, modify, merge, 14 | # publish, distribute, sublicense, and/or sell copies of the Software, 15 | # and to permit persons to whom the Software is furnished to do so, 16 | # subject to the following conditions: 17 | # 18 | # The above copyright notice and this permission notice shall be 19 | # included in all copies or substantial of portions the Software. 20 | # 21 | # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 22 | # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 23 | # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 24 | # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE 25 | # LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 26 | # OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 27 | # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 28 | 29 | namespace Data; 30 | 31 | require_once 'Data.Contract.IMaybe.php'; 32 | require_once 'Data.Maybe.Just.php'; 33 | require_once 'Data.Maybe.Nothing.php'; 34 | 35 | # A `Maybe` type encapsulates an optional value. A value of type `Maybe a` 36 | # either contains a value of type a (represented as `Just a`), or it is empty 37 | # (represented as `Nothing`). Using `Maybe` is a good way to deal with errors 38 | # or exceptional cases without resorting to drastic measures such as 39 | # `Data.Error`. 40 | #The `Maybe` type is also a monad. It is a simple kind of error monad, where 41 | # all errors are represented by `Nothing`. A richer error monad can be built 42 | # using the `Either` (`Data.Either`) type. 43 | abstract class Maybe extends DataTypes { 44 | # Equivalent to Haskell's `>>=` operator. Its first argument is a value in 45 | # a monadic type, its second argument is a function that maps from the 46 | # underlying type of the first argument to another monadic type, and its 47 | # results is in that other monadic type. 48 | abstract function bind($fn); # :: (Maybe a, Func) -> Maybe b 49 | 50 | # Extracts the element out of a `Just` and returns an error if its argument 51 | # is `Nothing`. 52 | abstract function fromJust(); # :: Maybe a -> a 53 | 54 | # Takes a `Maybe` value and a default value. If the `Maybe` is `Nothing`, it 55 | # returns the default values; otherwise, it returns the value contained in 56 | # the `Maybe`. 57 | abstract function fromMaybe($def); # :: (Maybe a, a) -> a 58 | 59 | # Returns `Bool (True)` if its argument is of the form `Just _`. 60 | abstract function isJust(); # :: Maybe a -> Bool 61 | 62 | # Returns `Bool (True)` if its arguments is of the form `Nothing`. 63 | abstract function isNothing(); # :: Maybe a -> Bool 64 | 65 | # Takes a default value, a function and, of course, a `Maybe` value. If the 66 | # `Maybe` value is `Nothing`, the function returns the default value. 67 | # Otherwise, it applies the function to the value inside the `Just` and 68 | # returns the result. 69 | abstract function maybe($def, $fn); # :: (Maybe a, b, Func) -> b 70 | 71 | # Returns an empty list when given ``Nothing`` or a singleton list when not 72 | # given ``Nothing``. 73 | abstract function toList(); # :: Maybe a -> Collection 74 | } 75 | 76 | # This function applies construction for `Maybe` monad. It is able to detect 77 | # and return `Just _` or `Nothing`, wrapping the value in a monadic 78 | # constructor in case of success. 79 | function Maybe($value) { 80 | # A value is nothing when it is null or instance of Data.Null. Otherwise, is 81 | # Just _. 82 | if (is_null($value) || is_object($value) and get_class($value) === "Data\\Null") 83 | return new \Data\Maybe\Nothing; 84 | return new \Data\Maybe\Just($value); 85 | } 86 | -------------------------------------------------------------------------------- /old/Data.Null.php: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/haskellcamargo/rawr/5f49fc8bdb0096db4d09b77d6260be6f7829ef89/old/Data.Null.php -------------------------------------------------------------------------------- /old/Data.Num.Contract.IFloat.php: -------------------------------------------------------------------------------- 1 | 3 | # 4 | # Permission is hereby granted, free of charge, to any person 5 | # obtaining a copy of this software and associated documentation files 6 | # (the "Software"), to deal in the Software without restriction, 7 | # including without limitation the rights to use, copy, modify, merge, 8 | # publish, distribute, sublicense, and/or sell copies of the Software, 9 | # and to permit persons to whom the Software is furnished to do so, 10 | # subject to the following conditions: 11 | # 12 | # The above copyright notice and this permission notice shall be 13 | # included in all copies or substantial of portions the Software. 14 | # 15 | # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 16 | # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 17 | # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 18 | # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE 19 | # LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 20 | # OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 21 | # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 22 | 23 | # Use memoization to store already processed data in a static variable 24 | # This way you can increase the performance in more than 95% of 25 | # already processed data. 26 | 27 | namespace Data\Num\Contract; 28 | 29 | interface IFloat { 30 | public function __construct($a); # :: (Enum a) => a -> a 31 | } -------------------------------------------------------------------------------- /old/Data.Num.Contract.IInt.php: -------------------------------------------------------------------------------- 1 | 3 | # 4 | # Permission is hereby granted, free of charge, to any person 5 | # obtaining a copy of this software and associated documentation files 6 | # (the "Software"), to deal in the Software without restriction, 7 | # including without limitation the rights to use, copy, modify, merge, 8 | # publish, distribute, sublicense, and/or sell copies of the Software, 9 | # and to permit persons to whom the Software is furnished to do so, 10 | # subject to the following conditions: 11 | # 12 | # The above copyright notice and this permission notice shall be 13 | # included in all copies or substantial of portions the Software. 14 | # 15 | # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 16 | # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 17 | # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 18 | # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE 19 | # LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 20 | # OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 21 | # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 22 | 23 | # Use memoization to store already processed data in a static variable 24 | # This way you can increase the performance in more than 95% of 25 | # already processed data. 26 | 27 | namespace Data\Num\Contract; 28 | use \Data\Num\Int; 29 | 30 | interface IInt { 31 | # | METHOD NAME | METHOD ARGUMENTS | TYPE SIGNATURE 32 | function __construct ($a); # :: a -> Int 33 | function mtRandUntil (Int &$n = Null); # :: (Int, Int) -> Int 34 | function mtSeedRand (); # :: Int -> Int 35 | function seedRand (); # :: Int -> Int 36 | function to (Int &$n); # :: (Int, Int) 37 | function toBin (); # :: Int -> Str 38 | function toHex (); # :: Int -> Str 39 | function toOct (); # :: Int -> Str 40 | } -------------------------------------------------------------------------------- /old/Data.Num.Float.php: -------------------------------------------------------------------------------- 1 | Marcelo Camargo 3 | # @contributors => [] 4 | # @creation_date => Unkown 5 | # @last_changed => 2015-02-24 6 | # @package => Data.Num.Float 7 | 8 | # Copyright (c) 2014 Marcelo Camargo 9 | # 10 | # Permission is hereby granted, free of charge, to any person 11 | # obtaining a copy of this software and associated documentation files 12 | # (the "Software"), to deal in the Software without restriction, 13 | # including without limitation the rights to use, copy, modify, merge, 14 | # publish, distribute, sublicense, and/or sell copies of the Software, 15 | # and to permit persons to whom the Software is furnished to do so, 16 | # subject to the following conditions: 17 | # 18 | # The above copyright notice and this permission notice shall be 19 | # included in all copies or substantial of portions the Software. 20 | # 21 | # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 22 | # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 23 | # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 24 | # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE 25 | # LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 26 | # OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 27 | # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 28 | 29 | namespace Data\Num; 30 | 31 | require_once 'Data.Num.Contract.IFloat.php'; 32 | use \Data\Num\Contract\IFloat as IFloat; 33 | 34 | class Float extends \Data\Num implements IFloat { 35 | public function __construct($v) { 36 | if (is_numeric($v)) 37 | $this->value = (float) $v; 38 | else 39 | throw new Exception("Expecting `{$v}` to be a numeric value. Instead" 40 | . " got " . gettype($v)); 41 | } 42 | 43 | public function diff(Float &$y) { # :: (Float, Float) -> Bool 44 | return Bool($this->value !== $y->value); 45 | } 46 | 47 | public function eq(Float &$y) { # :: (Float, Float) -> Bool 48 | return Bool($this->value === $y->value); 49 | } 50 | } 51 | -------------------------------------------------------------------------------- /old/Data.Num.Int.php: -------------------------------------------------------------------------------- 1 | Marcelo Camargo 3 | # @contributors => [] 4 | # @creation_date => Unkown 5 | # @last_changed => 2015-02-24 6 | # @package => Data.Num.Int 7 | 8 | # Copyright (c) 2014 Marcelo Camargo 9 | # 10 | # Permission is hereby granted, free of charge, to any person 11 | # obtaining a copy of this software and associated documentation files 12 | # (the "Software"), to deal in the Software without restriction, 13 | # including without limitation the rights to use, copy, modify, merge, 14 | # publish, distribute, sublicense, and/or sell copies of the Software, 15 | # and to permit persons to whom the Software is furnished to do so, 16 | # subject to the following conditions: 17 | # 18 | # The above copyright notice and this permission notice shall be 19 | # included in all copies or substantial of portions the Software. 20 | # 21 | # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 22 | # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 23 | # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 24 | # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE 25 | # LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 26 | # OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 27 | # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 28 | 29 | namespace Data\Num; 30 | use \Data\Str; 31 | use \Data\Num\Contract\IInt; 32 | 33 | require_once "Data.Num.Contract.IInt.php"; 34 | 35 | class Int extends \Data\Num implements IInt { 36 | public function __construct($v) { 37 | if (is_numeric($v)) 38 | $this->value = (int) $v; 39 | else 40 | throw new Exception("Expecting `{$v}` to be a numeric value. Instead" 41 | . " got " . gettype($v)); 42 | } 43 | 44 | # Generate a better random value. 45 | public function mtRandUntil(Int &$n = Null) { # :: (Int, Int) -> Int 46 | return new Int( 47 | mt_rand($this->value, $n === Null ? MT_RAND_MAX : $n->value)); 48 | } 49 | 50 | # Seeds the random number generator. 51 | public function mtSeedRand() { # :: Int -> Int 52 | mt_srand($this->value); 53 | return $this; 54 | } 55 | 56 | # Seeds the random number generator. 57 | public function seedRand() { # :: Int -> Int 58 | srand($this->value); 59 | return $this; 60 | } 61 | 62 | # Returns a list of integrals 63 | public function to(Int &$n) { # :: (Int, Int) -> [Int] 64 | return (new \Data\Collection(range($this->value, $input->value))) 65 | -> of ('Data.Num.Int'); 66 | } 67 | 68 | # Gives a string containing the binary conversion of the number. 69 | public function toBin() { # :: Int -> Str 70 | return new Str(decbin($this->value)); 71 | } 72 | 73 | # Gives a string containing the hexadecimal value of the number. 74 | public function toHex() { # :: Int -> Str 75 | return new Str(dechex($this->value)); 76 | } 77 | 78 | # Gives a string containing the octal value of the number. 79 | public function toOct() { # :: Int -> Str 80 | return new Str(decoct($this->value)); 81 | } 82 | } 83 | -------------------------------------------------------------------------------- /old/Data.Num.php: -------------------------------------------------------------------------------- 1 | Marcelo Camargo 3 | # @contributors => [] 4 | # @creation_date => Unkown 5 | # @last_changed => 2015-02-24 6 | # @package => Data.Num 7 | 8 | # Copyright (c) 2014 Marcelo Camargo 9 | # 10 | # Permission is hereby granted, free of charge, to any person 11 | # obtaining a copy of this software and associated documentation files 12 | # (the "Software"), to deal in the Software without restriction, 13 | # including without limitation the rights to use, copy, modify, merge, 14 | # publish, distribute, sublicense, and/or sell copies of the Software, 15 | # and to permit persons to whom the Software is furnished to do so, 16 | # subject to the following conditions: 17 | # 18 | # The above copyright notice and this permission notice shall be 19 | # included in all copies or substantial of portions the Software. 20 | # 21 | # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 22 | # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 23 | # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 24 | # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE 25 | # LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 26 | # OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 27 | # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 28 | 29 | namespace Data; 30 | 31 | require_once 'Data.Contract.INum.php'; 32 | use \Data\Contract\INum as INum; 33 | use \Exception; 34 | 35 | # This function is responsible by inference in numeric values. It is able to 36 | # return the value wrapped in the best type that it can determine for it. 37 | # Only numeric values are accepted. 38 | function Num($v) { 39 | switch (gettype($v)) { 40 | case "int": 41 | case "integer": 42 | return Int($v); 43 | case "float": 44 | case "double": 45 | case "real": 46 | return Float($v); 47 | default: 48 | throw new Exception("Expecting `{$val}` to be a valid number. Received " 49 | . gettype($val)); 50 | } 51 | } 52 | 53 | # The `Num` type holds numeric values. By default, `Int` and `Float` types 54 | # inherit from it. All basic operations that can be applied for both numeric 55 | # types are defined in this type. 56 | abstract class Num extends DataTypes { 57 | # Type rules are here specified. 58 | # PHP has variable variables, therefore, we just 59 | # return a primitive string saying the type we want 60 | # to return and after we search on classes where a class 61 | # is defined with the same name of the string and we make 62 | # a new instance of them to return. 63 | private function _return($operand) { 64 | if (($type = gettype($this->value)) === gettype($operand)) { 65 | switch ($type) { 66 | case "float": 67 | case "double": 68 | case "real": 69 | return "\\Data\\Num\\Float"; 70 | case "int": 71 | case "integer": 72 | return "\\Data\\Num\\Int"; 73 | } 74 | } else { 75 | $source = is_double($operand) ? "Data.Num.Float" : "Data.Num.Int"; 76 | $target = str_replace('\\', '.', get_class($this)); 77 | throw new \Exception( 78 | "Cannot implicity convert \"{$target}\" to \"{$source}\"."); 79 | } 80 | } 81 | 82 | # Returns the absolute value of a number. 83 | function abs() { # :: Num -> Num 84 | # This function always returns a number of the same type of the object 85 | # that has this behavior. 86 | $_ = get_class($this); 87 | return new $_(abs($this->value)); 88 | } 89 | 90 | # Adds a value to a number. 91 | function add(Num &$n) { # :: (Num a) => (a, a) -> a 92 | $_ = $this->_return($n->value); 93 | return new $_($this->value + $n->value); 94 | } 95 | 96 | # Arc cosin. 97 | function arcCos() { # :: Num -> Float 98 | return new Num\Float(acos($this->value)); 99 | } 100 | 101 | # Arc sin. 102 | function arcSin() { # :: Num -> Float 103 | return new Num\Float(asin($this->value)); 104 | } 105 | 106 | # Arc tangent of 2 values. 107 | function arcTan2(Num &$n) { # :: (Num, Num) -> Float 108 | return new Num\Float(atan2($this->value, $n->value)); 109 | } 110 | 111 | # Arc tangent. 112 | function arcTan() { # :: Num -> Float 113 | return new Num\Float(atan($this->value)); 114 | } 115 | 116 | # Round fractions up. 117 | function ceil() { # :: Num -> Float 118 | return new Num\Float(ceil($this->value)); 119 | } 120 | 121 | # Cosin. 122 | function cos() { # :: Num -> Float 123 | return new Num\Float(cos($this->value)); 124 | } 125 | 126 | # Degrees to radians. 127 | function degToRad() { # :: Num -> Float 128 | return new Num\Float(deg2rad($this->value)); 129 | } 130 | 131 | # Divides by the parameter. 132 | function div(Num &$n) { # :: (Num, Num) -> Float 133 | $_ = $this->_return($n()); 134 | return new $_($this->value / $n->value); 135 | } 136 | 137 | # Calculates the exponent of e. 138 | function exp() { # :: Num -> Float 139 | return new Num\Float(exp($this->value)); 140 | } 141 | 142 | # Returns exp(Number) - 1, computed in a way that is accurate even 143 | # when the value of number is close to zero. 144 | function expm1() { # :: Num -> Float 145 | return new Num\Float(expm1($this->value)); 146 | } 147 | 148 | # Round fractions down. 149 | function floor() { # :: Num -> Float 150 | return new Num\Float(floor($this->value)); 151 | } 152 | 153 | # Hyperbolic arc cosin, 154 | function hArcCos() { # :: Num -> Float 155 | return new Num\Float(acosh($this->value)); 156 | } 157 | 158 | # Hyperbolic arc sin. 159 | function hArcSin() { # :: Num -> Float 160 | return new Num\Float(asinh($this->value)); 161 | } 162 | 163 | # Hyperbolic arc tangent. 164 | function hArcTan() { # :: Num -> Float 165 | return new Num\Float(atanh($this->value)); 166 | } 167 | 168 | # Hyperbolic cosin. 169 | function hCos() { # :: Num -> Float 170 | return new Num\Float(cosh($this->value)); 171 | } 172 | 173 | # Hyperbolic sin. 174 | function hSin() { # :: Num -> Float 175 | return new Num\Float(sinh($this->value)); 176 | } 177 | 178 | # Hyperbolic tangent. 179 | function hTan() { # :: Num -> Float 180 | return new Num\Float(tanh($this->value)); 181 | } 182 | 183 | # Returns the hypotenuse of a triangle. 184 | function hypot(Num &$n) { # :: (Num, Num) -> Float 185 | return new Num\Float(hypot($this->value, $n->value)); 186 | } 187 | 188 | # Returns a boolean saying if the number is finite. 189 | function isFinite() { # :: Num -> Bool 190 | return new Bool(is_finite($this->value)); 191 | } 192 | 193 | # Returns a boolean saying if the number is infinite. 194 | function isInfinite() { # :: Num -> Bool 195 | return new Bool(is_infinite($this->value)); 196 | } 197 | 198 | # Returns true if the valus is not a number. 199 | function isNAN() { # :: Num -> Bool 200 | return new Bool(is_nan($this->value)); 201 | } 202 | 203 | # Base 10 logarithm. 204 | function log10() { # :: Num -> Float 205 | return new Num\Float(log10($this->value)); 206 | } 207 | 208 | # Returns log(1 + Number), computed in a way that is accurate even 209 | # when the value of number is close to zero. 210 | function log1p() { # :: Num -> Float 211 | return new Num\Float(log1p($this->value)); 212 | } 213 | 214 | # Natural logarithm. 215 | function log(Num &$n = Null) { # :: (Num, Num) -> Float 216 | return new Num\Float(log($this->value, $n === Null ? M_E : $n())); 217 | } 218 | 219 | # The module of the division. 220 | function mod(Num &$n) { # (Num, Num) -> Num 221 | $_ = $this->_return($n()); 222 | return new $_(fmod($this->value, $n())); 223 | } 224 | 225 | # Multiplication. 226 | function mul(Num &$n) { # :: (Num, Num) -> Num 227 | $_ = $this->_return($n()); 228 | return new $_($this->value * $n->value); 229 | } 230 | 231 | # Returns the negation of the value. 232 | function negate() { # :: Num -> Num 233 | return -$this->value; 234 | } 235 | 236 | # Exponential expression. 237 | function pow(Num &$n) { # :: (Num, Num) -> Num 238 | $_ = $this->_return($n->value); 239 | return new $_(pow($this->value, $a)); 240 | } 241 | 242 | # Converts the radian number to the equivalent number in degrees. 243 | function radToDeg() { # :: Num -> Float 244 | return new Num\Float(rad2deg($this->value)); 245 | } 246 | 247 | # Generate a random integer. 248 | function randUntil(Num &$n) { # :: (Num, Num) -> Int 249 | return new Num\Int(rand($this->value, $$n->value)); 250 | } 251 | 252 | # Rounds a float. 253 | # Uncle Rasmus doesn't allow returned functions to be applied. 254 | # Some day I'll throw a pie in Lerdorf's face by this. 255 | # That's why round isn't an unary function and will not allow currying. 256 | function round(Int &$n = Null, Int &$o = Null) { 257 | # :: (Num, Int, Int) -> Float 258 | return new Num\Float( 259 | round($this->value 260 | , $n === Null ? 0 /* otherwise */ : $n->value 261 | , $o === Null ? PHP_ROUND_HALF_UP : $o->value)); 262 | } 263 | 264 | # Sin. 265 | function sin() { # :: Num -> Float 266 | return new Num\Float(sin($this->value)); 267 | } 268 | 269 | # Square root of the number. 270 | function sqrt() { # :: Num -> Float 271 | return new Num\Float(sqrt($this->value)); 272 | } 273 | 274 | # Subtraction 275 | function sub(Num &$n) { # :: (Num, Num) -> Num 276 | $_ = $this->_return($n()); 277 | return new $_($this->value - $n->value); 278 | } 279 | 280 | # Tangent. 281 | function tan() { # :: Num -> Float 282 | return new Num\Float(tan($this->value)); 283 | } 284 | } 285 | -------------------------------------------------------------------------------- /old/Data.Object.php: -------------------------------------------------------------------------------- 1 | 3 | # 4 | # Permission is hereby granted, free of charge, to any person 5 | # obtaining a copy of this software and associated documentation files 6 | # (the "Software"), to deal in the Software without restriction, 7 | # including without limitation the rights to use, copy, modify, merge, 8 | # publish, distribute, sublicense, and/or sell copies of the Software, 9 | # and to permit persons to whom the Software is furnished to do so, 10 | # subject to the following conditions: 11 | # 12 | # The above copyright notice and this permission notice shall be 13 | # included in all copies or substantial of portions the Software. 14 | # 15 | # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 16 | # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 17 | # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 18 | # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE 19 | # LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 20 | # OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 21 | # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 22 | 23 | namespace Data; 24 | use \Exception; 25 | 26 | class Object extends DataTypes { 27 | public $prototype = null; 28 | private $typeOf = []; 29 | 30 | public function __call($name, $arguments) { 31 | array_unshift($arguments, $this); 32 | if (is_object($this->{$name})) 33 | return call_user_func_array($this->{$name}->value(), $arguments); 34 | else 35 | return call_user_func_array($this->{$name}, $arguments); 36 | } 37 | 38 | public function __construct($v = []) { 39 | if (gettype($v) === "array" && !empty($v)) 40 | foreach ($v as $property => $type) 41 | if (is_string($property)) 42 | $this->typeOf[$property] = $type; 43 | else 44 | throw new Exception("Not an identifier: {$property}"); 45 | } 46 | 47 | public function __set($property, $value) { 48 | if (isset($this->typeOf[$property])) 49 | if (is_array($this->typeOf[$property]) && sizeof($this->typeOf[$property])) { 50 | 51 | } else # PHP && operator works like Erlang's andelse. 52 | if (is_object($value) && get_class($value) == $this->typeOf[$property]) # Rawr object, no need of optimization 53 | $this->{$property} = $value; 54 | else { 55 | $className = "\\" . $this->typeOf[$property]; 56 | $this->{$property} = new $className($value); 57 | } 58 | else 59 | throw new Exception("Object has no property '{$property}'."); 60 | } 61 | } -------------------------------------------------------------------------------- /old/Data.Str.php: -------------------------------------------------------------------------------- 1 | 3 | # 4 | # Permission is hereby granted, free of charge, to any person 5 | # obtaining a copy of this software and associated documentation files 6 | # (the "Software"), to deal in the Software without restriction, 7 | # including without limitation the rights to use, copy, modify, merge, 8 | # publish, distribute, sublicense, and/or sell copies of the Software, 9 | # and to permit persons to whom the Software is furnished to do so, 10 | # subject to the following conditions: 11 | # 12 | # The above copyright notice and this permission notice shall be 13 | # included in all copies or substantial of portions the Software. 14 | # 15 | # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 16 | # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 17 | # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 18 | # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE 19 | # LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 20 | # OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 21 | # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 22 | 23 | namespace Data; 24 | 25 | require_once 'Data.Contract.IStr.php'; 26 | use \TypeClass\Eq as Eq; 27 | 28 | class Str extends DataTypes { 29 | # By default, data types that inherit from this class 30 | # and don't override the constructor must pass the internal 31 | # value of the variable in it. 32 | function __construct($val) { # :: a -> String 33 | parent :: __construct(); # Apply memoization 34 | $this->value = (string) $val; 35 | } 36 | 37 | # Returns the string with the first character converted to 38 | # uppercase and the remainder to lowercase. Note: case 39 | # conversion is effective only in ASCII region 40 | function capitalize() { # :: Str -> Str 41 | return new Str(ucwords($this->value)); 42 | } 43 | 44 | # Returns an array of characters in string. 45 | function chars() { # :: Str -> Collection 46 | return new Collection(str_split($this->value)); 47 | } 48 | 49 | # Returns a one-character string at the beginning of the string. 50 | function chr() { # :: Str -> Str 51 | return new Str($this->value[0]); 52 | } 53 | 54 | # Makes string empty 55 | function clear() { # :: Str -> Str 56 | return new Str(""); 57 | } 58 | 59 | # Returns an array of the Integer ordinals of the characters in 60 | function codePoints() { # :: Str -> Collection 61 | $acc = []; 62 | for ($i = 0, $len = strlen($this->value); $i < $len; $i++) 63 | $acc[] = (int) ord($this->value[$i]); 64 | return new Collection($acc); 65 | } 66 | 67 | # Append the given object to string. If object is an Integer, 68 | # it is considered as a codepoint, and is converted to a character 69 | # before concatenation. 70 | function concat() { # :: (Str ...) -> Str 71 | list ($let['acc'] 72 | , $let['args']) = [$this->value, func_get_args()]; 73 | 74 | for ($i = 0, $len = func_num_args(); $i < $len; $i++) 75 | if (is_integer($let['args'][$i]) || $let['args'][$i] instanceof \Data\Num\Int) { 76 | $let['acc'] .= gettype($let['args'][$i]) === "object" ? 77 | chr((string) $let['args'][$i]) 78 | : chr($let['args'][$i]); 79 | } else $let['acc'] .= $let['args'][$i]; 80 | return new Str($let['acc']); 81 | } 82 | 83 | # Downcases a string. 84 | function toLower() { # :: Str -> Str 85 | return new Str(strtolower($this->value)); 86 | } 87 | 88 | # Different of. 89 | function diff(Str $s) { # :: (Str, Str) -> Bool 90 | return new Bool((string) $this !== (string) $s); 91 | } 92 | 93 | # Applies a function to each char in the string 94 | function eachChar(Func $func) { # :: (Str, Func) -> Str 95 | $this 96 | -> chars () 97 | -> each ($func); 98 | return $this; 99 | } 100 | 101 | # Applies a function to each codepoint in the string 102 | function eachCodePoint(Func $func) { # :: (Str, Func) -> Str 103 | $this 104 | -> codePoints () 105 | -> each ($func); 106 | return $this; 107 | } 108 | 109 | # Applies a function to each line 110 | function eachLine(Func $func) { # :: (Str, Func) -> Str 111 | $this 112 | -> lines () 113 | -> each ($func); 114 | } 115 | 116 | # Equals to. 117 | function eq(Str $s) { # :: (Str, Str) -> Bool 118 | return new Bool((string) $this === (string) $s); 119 | } 120 | 121 | # Returns if the current string value is prefix of the parameter 122 | function isPrefixOf(Str $of) { # :: (Str, Str) -> Bool 123 | # Re-implementation of comparasion basis 124 | # Non-deterministic parsing. Faster than using preg_match 125 | # [pass] => [pass] => [pass] => [pass] ... 126 | # => [fail] => [break] 127 | # 128 | # Haskell-equivalent: 129 | # isPrefixOf :: [Char] -> [Char] -> Bool 130 | # isPrefixOf [] _ = True 131 | # isPrefixOf (x:xs) (y:ys) 132 | # | x == y = isPrefixOf xs ys 133 | # | otherwise = False 134 | # 135 | # Apply this function by use of recursion: 136 | $isPrefixOf = function ($prefix, $string) use (&$isPrefixOf) { 137 | # Edge condition: 138 | if ($prefix == "") 139 | return new Bool(True); 140 | 141 | # Compare head. 142 | if ($prefix[0] === $string[0]) 143 | return $isPrefixOf(substr($prefix, 1), substr($string, 1)); 144 | 145 | return new Bool(False); 146 | }; 147 | 148 | return $isPrefixOf($this->value, $of->value); 149 | } 150 | 151 | # Returns if the current string value is suffix of the parameter 152 | function isSuffixOf(Str $of) { # :: (Str, Str) -> Bool 153 | $isPrefixOf = function ($prefix, $string) use (&$isPrefixOf) { 154 | # Edge condition: 155 | if ($prefix == "") 156 | return new Bool(True); 157 | 158 | # Compare head. 159 | if ($prefix[0] === $string[0]) 160 | return $isPrefixOf(substr($prefix, 1), substr($string, 1)); 161 | 162 | return new Bool(False); 163 | }; 164 | 165 | return $isPrefixOf(strrev($this->value), strrev((string) $of)); 166 | } 167 | 168 | # Joins a list with the specified separator 169 | function join(Collection $arr) { # :: (Str, Collection) -> Str 170 | return new Str(join($this->value, TypeInference :: toPrimitive($arr))); 171 | } 172 | 173 | # Returns the length of a string 174 | function length() { # :: Str -> Int 175 | return new Num\Int(strlen($this->value)); 176 | } 177 | 178 | # Splits a string at newlines into a list. 179 | # String → [String] 180 | function lines() { 181 | return (new Collection( 182 | preg_replace("/\t/", "", explode("\n", $this->value)))) -> of ('Data.Str'); 183 | } 184 | 185 | # Outputs to screen a string and doesn't break line. 186 | function putStr() { # :: Str -> Str 187 | echo $this->value; 188 | return $this; 189 | } 190 | 191 | # Outputs to screen a string and breaks line. 192 | function putStrLn() { # :: Str -> Str 193 | echo $this->value . "\n"; 194 | return $this; 195 | } 196 | 197 | # Returns a collection of the string separated by the 198 | # given pattern. 199 | function split(Str $pattern) { # :: (Str, Str) -> Collection 200 | return new Collection(split((string) $pattern, $this->value)); 201 | } 202 | 203 | # Takes its second argument, and repeats it n times to create a new, 204 | # single, string. 205 | function repeat($times) { # :: (Str, Int) -> Str 206 | $let['acc'] = $this->value; 207 | for ($i = 0; $i < $times - 1; $i++) 208 | $let['acc'] .= $this->value; 209 | return new Str($let['acc']); 210 | } 211 | 212 | # Reverses a string. 213 | function reverse() { # :: Str -> Str 214 | $memoize = $this->memoize; 215 | $reverse = $memoize (function() { 216 | return strrev($this->value); 217 | }); 218 | return new Str($reverse()); 219 | } 220 | 221 | # Converts a string to uppercase. 222 | function toUpper() { # :: Str -> Str 223 | return new Str(strtoupper($this->value)); 224 | } 225 | 226 | # Swaps the case of a string. 227 | function swapCase() { # :: Str -> Str 228 | $strcopy = $this->value; 229 | for ($i = 0, $len = strlen($strcopy); $i < $len; $i++) 230 | if (preg_match('/[a-z]/', $strcopy[$i])) 231 | $strcopy[$i] = strtoupper($strcopy[$i]); 232 | else if (preg_match('/[A-Z]/', $strcopy[$i])) 233 | $strcopy[$i] = strtolower($strcopy[$i]); 234 | return new Str($strcopy); 235 | } 236 | 237 | # Splits a string at spaces (one or more), returning 238 | # a list of strings 239 | function words() { # :: Str -> Collection 240 | return (new Collection (explode(' ', $this->value))) 241 | -> of ('Data.Str'); 242 | } 243 | } -------------------------------------------------------------------------------- /old/Data.Test.hs: -------------------------------------------------------------------------------- 1 | send (); } 14 | 15 | $t = Match ($name) -> with ([ 16 | "Str x" => function($x) { return Str ("Eu te amo, ") -> concat ($x); } 17 | ]); 18 | -------------------------------------------------------------------------------- /old/Data.Tuple.php: -------------------------------------------------------------------------------- 1 | Marcelo Camargo 3 | # @contributors => [] 4 | # @creation_date => Unkown 5 | # @last_changed => 2015-02-23 6 | # @package => Data.Tuple 7 | 8 | # Copyright (c) 2014 Marcelo Camargo 9 | # 10 | # Permission is hereby granted, free of charge, to any person 11 | # obtaining a copy of this software and associated documentation files 12 | # (the "Software"), to deal in the Software without restriction, 13 | # including without limitation the rights to use, copy, modify, merge, 14 | # publish, distribute, sublicense, and/or sell copies of the Software, 15 | # and to permit persons to whom the Software is furnished to do so, 16 | # subject to the following conditions: 17 | # 18 | # The above copyright notice and this permission notice shall be 19 | # included in all copies or substantial of portions the Software. 20 | # 21 | # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 22 | # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 23 | # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 24 | # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE 25 | # LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 26 | # OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 27 | # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 28 | 29 | namespace Data; 30 | 31 | require_once 'Data.Contract.ITuple.php'; 32 | 33 | # The `Tuple` type is a different way of storing multiple values in a single 34 | # value. The main differences between tuples and lists are that tuples have a 35 | # fixed number of elements (immutable); therefore, it makes sense to use 36 | # tuples when you known in advance how many values are to be stored. For 37 | # example, we might want a type for storing 2D coordinates of a point. We 38 | # known exactly how many values we need for each point (two - the x and y 39 | # coordinates), so tuples are applicable. The elements of a tuple do not need 40 | # to be all of the same type. For instance, in a phonebook application we 41 | # might want to handle the entries by crunching three values into one: the 42 | # name, phone number, and the address of each person. In such a case, the 43 | # three values won't have the same type, so lists wouldn't help, but tuples 44 | # would. 45 | class Tuple extends DataTypes implements Contract\ITuple { 46 | # $size determines the size that a tuple have, statically. This value is 47 | # immutable. $type represents a list containing the type of each element. 48 | private $size = 0 49 | , $type = []; 50 | 51 | # Constructor is variadic. ITuples should be classified as dependent types. 52 | # ONLY objects can be put inside tuples. This is made to avoid workarounds 53 | # that the programmer can do. 54 | function __construct() { 55 | list ($this->size 56 | , $arguments) = [count($a = func_get_args()[0]), $a]; 57 | 58 | foreach ($arguments as $item) 59 | $this->type[] = parent :: typeName(get_class($item)); 60 | 61 | $this->value = $arguments; 62 | } 63 | 64 | # Returns `Just` the first element of a tuple (if it exists), or `Nothing`. 65 | function fst() { # :: Tuple(Int) -> Maybe a 66 | return isset($this->value[0])? Just($this->value[0]) 67 | /* otherwise */ : Nothing(); 68 | } 69 | 70 | # Works like a 1-indexed array, where you get `Just` the element in the 71 | # received index or `Nothing`. 72 | function get(Num\Int $index) { # :: (Tuple, Int) -> Maybe a 73 | $index = $index->value - 1; 74 | return isset($this->value[$index])? Just($this->value[$index]) 75 | /* otherwise */ : Nothing(); 76 | } 77 | 78 | # Returns `Just` the second element of a tuple (if it exists), or `Nothing`. 79 | function snd() { # :: Tuple(Int) -> Maybe b 80 | return isset($this->value[1])? Just($this->value[1]) 81 | /* otherwise */ : Nothing(); 82 | } 83 | 84 | # Returns the type of the items contained in the tuples in format of a 85 | # `Data.Str`. 86 | function showType() { # :: Tuple(Int) -> Maybe b 87 | return new \Data\Str( 88 | "Tuple<" . implode(", ", $this->type) . ">({$this->size})"); 89 | } 90 | 91 | # Swaps the values of a pair and returns `Just` the tuple or returns 92 | # `Nothing` in case of not-a-pair. 93 | function swap() { # :: Tuple -> Maybe Tuple 94 | if ($this->size == 2) 95 | return Just(Tuple($this->value[1] 96 | , $this->value[0])); 97 | return Nothing(); 98 | } 99 | } 100 | -------------------------------------------------------------------------------- /old/Data.Types.php: -------------------------------------------------------------------------------- 1 | 3 | # 4 | # Permission is hereby granted, free of charge, to any person 5 | # obtaining a copy of this software and associated documentation files 6 | # (the "Software"), to deal in the Software without restriction, 7 | # including without limitation the rights to use, copy, modify, merge, 8 | # publish, distribute, sublicense, and/or sell copies of the Software, 9 | # and to permit persons to whom the Software is furnished to do so, 10 | # subject to the following conditions: 11 | # 12 | # The above copyright notice and this permission notice shall be 13 | # included in all copies or substantial of portions the Software. 14 | # 15 | # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 16 | # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 17 | # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 18 | # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE 19 | # LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 20 | # OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 21 | # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 22 | 23 | namespace Data; 24 | header("X-Powered-By: RAWR/1.0.0.3"); 25 | # Typeclasses 26 | 27 | require_once "typeclasses/Eq.interface.php"; 28 | require_once "typeclasses/Ord.interface.php"; 29 | require_once "typeclasses/Eq.php"; 30 | require_once "typeclasses/Ord.php"; 31 | 32 | require_once 'Data.Error.php'; 33 | require_once 'Data.Bool.php'; 34 | 35 | # If you don't want the entire world to burn down in flames, DON'T REMOVE THIS INCLUSION: 36 | require_once 'typing/TypeInference.class.php'; 37 | 38 | require_once 'Data.Match.php'; 39 | # Parent class for numeric types 40 | require_once 'Data.Num.php'; 41 | //require_once 'Data.Collection.php'; 42 | require_once 'Data.Func.php'; 43 | require_once 'Data.Maybe.php'; 44 | require_once 'Data.Either.php'; 45 | require_once 'Data.Num.Int.php'; 46 | require_once 'Data.Object.php'; 47 | require_once 'Data.Num.Float.php'; 48 | require_once 'Data.Str.php'; 49 | require_once 'Data.Tuple.php'; 50 | 51 | require_once 'extras/Shortcuts.php'; 52 | require_once 'modules/Memoize.class.php'; 53 | 54 | require_once 'extras/Constants.php'; 55 | 56 | # Types may want to inherit from this class. 57 | class Datatypes { 58 | protected $value, $memoize; 59 | 60 | public function __construct() { 61 | global $memoize; 62 | $this->memoize = $memoize; 63 | return $this; 64 | } 65 | 66 | // public function __call($name, $arguments) { # :: (a, string, array) -> mixed 67 | // array_unshift($arguments, $this); 68 | // return call_user_func_array($this->prototype->{$name}, $arguments); 69 | // } 70 | 71 | public function __clone() { # :: a -> void 72 | $this->prototype = clone $this->prototype; 73 | } 74 | 75 | public function __invoke() { # :: a -> a 76 | return $this->value; 77 | } 78 | 79 | public function __toString() { # :: a -> string 80 | return (string) $this->value; 81 | } 82 | 83 | # Equivalent to php's var_dump in the object. 84 | final public function about() { # :: a -> object 85 | var_dump($this); 86 | return $this; 87 | } 88 | 89 | # Applies a function or a set of instructions of the object 90 | public function apply($functions) { # :: (a, Str) -> a 91 | $let = [ 92 | "refl" => new \ReflectionClass(get_class($this)) 93 | , "func" => array_reverse(explode(" . ", $functions)) 94 | ]; 95 | $stack = $this; 96 | 97 | foreach ($let['func'] as $func) 98 | if ($let['refl'] -> hasMethod($func)) 99 | $stack = (new $let['refl'] -> name ($stack -> value())) -> {$func}(); 100 | else 101 | throw new \Exception("Object of type {$let['refl'] -> name} has no method {$func}."); 102 | 103 | return $stack; 104 | } 105 | 106 | # Equivalent to cond or switch 107 | public function caseOf($of) { 108 | foreach ($of as $case => $ret) 109 | if ($this->value == $case) 110 | return TypeInference :: infer($ret); 111 | return TypeInference :: infer($of[otherwise]); 112 | } 113 | 114 | # Returns the element by itself. 115 | public function id() { # :: a -> a 116 | return $this; 117 | } 118 | 119 | # Equivalent to php's var_dump. 120 | public function inspect() { # :: object 121 | var_dump($this->value); 122 | return $this; 123 | } 124 | 125 | # Casting to Bool. 126 | public function toBool() { # :: a -> String 127 | return new Bool($this->value); 128 | } 129 | 130 | # Casting to Collection. 131 | public function toCollection() { # :: a -> String 132 | return new Collection([$this->value]); 133 | } 134 | 135 | # Casting to Error. 136 | public function toError() { # :: a -> Error 137 | return new Error($this->value); 138 | } 139 | 140 | # Casting to File (path). 141 | public function toFile() { # :: a -> File 142 | return new File($this->value); 143 | } 144 | 145 | # Casting to Func. 146 | public function toFunc() { # :: a -> Func 147 | return new Func($this->value); 148 | } 149 | 150 | # Casting to Null. 151 | public function toNull() { # :: a -> Null 152 | return new Null; 153 | } 154 | 155 | # Casting to Num. 156 | public function toNum() { # :: a -> Num 157 | return new Num($this->value); 158 | } 159 | 160 | # Casting to Float. 161 | public function toFloat() { # :: a -> Float 162 | return new Num\Float($this->value); 163 | } 164 | 165 | # Casting to Int. 166 | public function toInt() { # :: a -> Int 167 | return new Num\Int($this->value); 168 | } 169 | 170 | # Casting to Object. 171 | public function toObject() { # :: a -> Object 172 | return new Object($this->value); 173 | } 174 | 175 | # Casting to String. 176 | public function toString() { # :: a -> String 177 | return new Str($this->value); 178 | } 179 | 180 | # Casting to Undefined (unset value). 181 | public function toUndefined() { # :: a -> Undefined 182 | return new Undefined; 183 | } 184 | 185 | # Casting to Void (return-less). 186 | public function toVoid() { # :: a -> Void 187 | return new Void; 188 | } 189 | 190 | # Replaces backslashes by dots. 191 | public static function typeName($t) { # :: (a, string) -> string 192 | return str_replace("\\", ".", $t); 193 | } 194 | 195 | # Returns the protected value as a php primitive. 196 | public function value() { # :: a -> a 197 | return $this->value; 198 | } 199 | } 200 | -------------------------------------------------------------------------------- /old/Data.Undefined.php: -------------------------------------------------------------------------------- 1 | 3 | # 4 | # Permission is hereby granted, free of charge, to any person 5 | # obtaining a copy of this software and associated documentation files 6 | # (the "Software"), to deal in the Software without restriction, 7 | # including without limitation the rights to use, copy, modify, merge, 8 | # publish, distribute, sublicense, and/or sell copies of the Software, 9 | # and to permit persons to whom the Software is furnished to do so, 10 | # subject to the following conditions: 11 | # 12 | # The above copyright notice and this permission notice shall be 13 | # included in all copies or substantial of portions the Software. 14 | # 15 | # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 16 | # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 17 | # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 18 | # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE 19 | # LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 20 | # OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 21 | # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 22 | 23 | namespace Data; 24 | use \TypeClass\Eq as Eq; 25 | 26 | final class Undefined extends DataTypes { 27 | public function __construct() { # :: a -> a 28 | unset($this->memoize); 29 | unset($this->prototype); 30 | unset($this->value); 31 | } 32 | 33 | # Different of. Requires all the values to be of the same type 34 | # and derived from Eq typeclass. 35 | function diff(Undefined $y) { # :: (Eq a) => (a, a) -> Bool 36 | # Taking account they have no value and the constraint explicit 37 | # accepts only Data.Undefined types, this will ALWAYS return false. 38 | return new Bool(False); 39 | } 40 | 41 | # Equals to, but requires both values to be of the same type and 42 | # derived from Eq typeclass. 43 | function eq(Undefined $y) { # :: (Eq a) => (a, a) -> Bool 44 | # Same note taken to diff, but true. 45 | return new Bool(True); 46 | } 47 | } -------------------------------------------------------------------------------- /old/Data.Void.php: -------------------------------------------------------------------------------- 1 | 3 | # 4 | # Permission is hereby granted, free of charge, to any person 5 | # obtaining a copy of this software and associated documentation files 6 | # (the "Software"), to deal in the Software without restriction, 7 | # including without limitation the rights to use, copy, modify, merge, 8 | # publish, distribute, sublicense, and/or sell copies of the Software, 9 | # and to permit persons to whom the Software is furnished to do so, 10 | # subject to the following conditions: 11 | # 12 | # The above copyright notice and this permission notice shall be 13 | # included in all copies or substantial of portions the Software. 14 | # 15 | # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 16 | # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 17 | # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 18 | # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE 19 | # LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 20 | # OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 21 | # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 22 | 23 | namespace Data; 24 | use \TypeClass\Eq as Eq; 25 | 26 | final class Void extends DataTypes { 27 | public function __construct() { # :: a -> a 28 | unset($this->memoize); 29 | unset($this->prototype); 30 | unset($this->value); 31 | } 32 | 33 | # Different of. Requires all the values to be of the same type 34 | # and derived from Eq typeclass. 35 | function diff(Void &$y) { # :: (Eq a) => (a, a) -> Bool 36 | # Taking account they have no value and the constraint explicit 37 | # accepts only Data.Void types, this will ALWAYS return false. 38 | return new Bool(False); 39 | } 40 | 41 | # Equals to, but requires both values to be of the same type and 42 | # derived from Eq typeclass. 43 | function eq(Void &$y) { # :: (Eq a) => (a, a) -> Bool 44 | # Same note taken to diff, but true. 45 | return new Bool(True); 46 | } 47 | } -------------------------------------------------------------------------------- /old/TODO.ml: -------------------------------------------------------------------------------- 1 | -- TODO SOON: 2 | - Refactorate the lexer, in ./patternMatching/. 3 | - Comment the lexer source. 4 | - Write the parser to generate the AST. 5 | - Create an interface for calling the lexer + parser and returning the ast. 6 | - Create a PHP-object to pattern-matching AST translator. 7 | 8 | 9 | progress: { 10 | todo: { 11 | types: [ 12 | "Data.Collection" 13 | , "Data.Error" 14 | , "Data.File" 15 | , "Data.Func" 16 | , "Data.Match" 17 | , "Data.Object" 18 | , "Data.Str" 19 | , "Data.Type" 20 | ] 21 | } 22 | , ready: { 23 | types: [ 24 | "Data.Bool" 25 | , "Data.Either" 26 | , "Data.Either.Left" 27 | , "Data.Either.Right" 28 | , "Data.Maybe" 29 | , "Data.Maybe.Just" 30 | , "Data.Maybe.Nothing" 31 | , "Data.Num" 32 | , "Data.Num.Float" 33 | , "Data.Num.Int" 34 | , "Data.Tuple" 35 | ] 36 | , contracts: [] 37 | } 38 | , documented: { 39 | types: [ 40 | "Data.Bool" 41 | , "Data.Either" 42 | , "Data.Maybe" 43 | , "Data.Tuple" 44 | ] 45 | , contracts: [] 46 | } 47 | } 48 | -------------------------------------------------------------------------------- /old/contributors.hs: -------------------------------------------------------------------------------- 1 | module Contributors where 2 | 3 | data Programmer = Programmer { problems :: [String] } 4 | 5 | marceloCamargo :: Programmer 6 | rafaelBernardo :: Programmer 7 | -------------------------------------------------------------------------------- /old/docgen/imperactiveTests.php: -------------------------------------------------------------------------------- 1 | methods = $reflectionClass->getMethods(); 39 | } 40 | else 41 | throw new Exception("Class '{$className}' not found."); 42 | } 43 | 44 | function count() { 45 | return $this->publicMethods; 46 | } 47 | 48 | function listMethods() { 49 | return $this->methods; 50 | } 51 | 52 | function listPublicMethods() { 53 | return filter(function (\ReflectionMethod $method) { 54 | return $method->isPublic(); 55 | }, $this->methods); 56 | } 57 | 58 | static function parseDocComments($doc) { 59 | list ($doc, $res) = [explode("\n", $doc), []]; 60 | foreach ($doc as $line) { 61 | $line = trim($line); 62 | if ($line == "/**" || $line == "*/") 63 | continue; 64 | /* otherwise */ 65 | if ($line[0] == "*") { 66 | $simpleData = "/\*\s*@([a-z\|]+)\s*:\s*(.*)/"; 67 | $out = null; 68 | $match = preg_match($simpleData, $line, $out); 69 | if ($match) { 70 | $res[] = [$out[1], $out[2]]; 71 | # Continue here by merging arrays with same key. 72 | } 73 | continue; 74 | } 75 | throw new Exception("Expecting '*' as the first character of line."); 76 | } 77 | return $res; 78 | } 79 | } 80 | 81 | class Method { 82 | public $name, $class, $type, $about; 83 | } 84 | 85 | $methodList = []; 86 | 87 | # Application 88 | 89 | $data["num"] = new ClassHolder("\\Data\\Num"); 90 | 91 | each(function ($item) { 92 | $t = ClassHolder :: parseDocComments($item->getDocComment()); 93 | var_dump($t); 94 | }, filter (function (\ReflectionMethod $method) { 95 | return $method->getDocComment(); 96 | }, $data["num"]->listPublicMethods())); 97 | -------------------------------------------------------------------------------- /old/extras/Constants.php: -------------------------------------------------------------------------------- 1 | 3 | # 4 | # Permission is hereby granted, free of charge, to any person 5 | # obtaining a copy of this software and associated documentation files 6 | # (the "Software"), to deal in the Software without restriction, 7 | # including without limitation the rights to use, copy, modify, merge, 8 | # publish, distribute, sublicense, and/or sell copies of the Software, 9 | # and to permit persons to whom the Software is furnished to do so, 10 | # subject to the following conditions: 11 | # 12 | # The above copyright notice and this permission notice shall be 13 | # included in all copies or substantial of portions the Software. 14 | # 15 | # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 16 | # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 17 | # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 18 | # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE 19 | # LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 20 | # OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 21 | # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 22 | 23 | define("MT_RAND_MAX", mt_getrandmax()); 24 | define("RAND_MAX", getrandmax()); 25 | define("RAWR_VERSION", "1.0.0.2"); 26 | 27 | # New Keywords 28 | # true and false must be instances of TrueClass and FalseClass 29 | define("No", false); 30 | define("Off", false); 31 | define("On", true); 32 | define("Yes", true); 33 | define("otherwise", "[:otherwise:]"); 34 | define("_", "[:otherwise:]"); 35 | 36 | /* Take care with the purism. Hitler started this way (Linspector, Torrens). */ 37 | define("Maybe", mt_rand() /* Change this */); 38 | 39 | # Type-safety for objects and default values for empty objects. 40 | 41 | /************** ALIAS ********** FOR TYPE ***********/ 42 | /**/ define("Bool", "Data\\Bool"); /**/ 43 | /**/ define("Boolean", "Data\\Bool"); /**/ 44 | /**/ define("Collection", "Data\\Collection"); /**/ 45 | /**/ define("Error", "Data\\Error"); /**/ 46 | /**/ define("File", "Data\\File"); /**/ 47 | /**/ define("Func", "Data\\Func"); /**/ 48 | /**/ define("Float", "Data\\Num\\Float"); /**/ 49 | /**/ define("Int", "Data\\Num\\Int"); /**/ 50 | /**/ define("Null", "Data\\Null"); /**/ 51 | /**/ define("Num", "Data\\Num"); /**/ 52 | /**/ define("Str", "Data\\Str"); /**/ 53 | /**/ define("String", "Data\\Str"); /**/ 54 | /**/ define("Undefined", "Data\\Undefined"); /**/ 55 | /**/ define("Void", "Data\\Void"); /**/ 56 | /****************************************************/ 57 | -------------------------------------------------------------------------------- /old/extras/Shortcuts.php: -------------------------------------------------------------------------------- 1 | 3 | # 4 | # Permission is hereby granted, free of charge, to any person 5 | # obtaining a copy of this software and associated documentation files 6 | # (the "Software"), to deal in the Software without restriction, 7 | # including without limitation the rights to use, copy, modify, merge, 8 | # publish, distribute, sublicense, and/or sell copies of the Software, 9 | # and to permit persons to whom the Software is furnished to do so, 10 | # subject to the following conditions: 11 | # 12 | # The above copyright notice and this permission notice shall be 13 | # included in all copies or substantial of portions the Software. 14 | # 15 | # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 16 | # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 17 | # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 18 | # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE 19 | # LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 20 | # OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 21 | # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 22 | 23 | function λ($v) { 24 | return new \Data\Func($v); 25 | } 26 | 27 | function §() { 28 | return new \Data\Collection(func_get_args()); 29 | } 30 | 31 | function bool($v) { 32 | return new \Data\Bool($v); 33 | } 34 | 35 | function boolean($v) { 36 | return new \Data\Bool($v); 37 | } 38 | 39 | function collection() { 40 | return new \Data\Collection(func_get_args()); 41 | } 42 | 43 | function double($v) { 44 | return new \Data\Num\Float($v); 45 | } 46 | 47 | function dynamic($v) { 48 | return \Data\TypeInference :: infer($v); 49 | } 50 | 51 | function false() { 52 | return new \Data\Bool\FalseClass; 53 | } 54 | 55 | function either($v) { 56 | return \Data\Either($v); 57 | } 58 | 59 | function error($msg, $code) { 60 | return new \Data\Error($msg, $code); 61 | } 62 | 63 | function float($v) { 64 | return new \Data\Num\Float($v); 65 | } 66 | 67 | function func($v) { 68 | return new \Data\Func($v); 69 | } 70 | 71 | function int($v) { 72 | return new \Data\Num\Int($v); 73 | } 74 | 75 | function integer($v) { 76 | return new \Data\Num\Int($v); 77 | } 78 | 79 | function just($v) { 80 | return new \Data\Maybe\Just($v); 81 | } 82 | 83 | function lambda($v) { 84 | return new \Data\Func($v); 85 | } 86 | 87 | function left($v) { 88 | return new \Data\Either\Left($v); 89 | } 90 | 91 | function match($v) { 92 | return new \Data\Match($v); 93 | } 94 | 95 | function maybe($v) { 96 | return \Data\Maybe($v); 97 | } 98 | 99 | function nothing() { 100 | return new \Data\Maybe\Nothing; 101 | } 102 | 103 | function num($v) { 104 | return \Data\Num($v); 105 | } 106 | 107 | function number($v) { 108 | return new \Data\Num($v); 109 | } 110 | 111 | function obj($v) { 112 | return new \Data\Object($v); 113 | } 114 | 115 | function object($v) { 116 | return new \Data\Object($v); 117 | } 118 | 119 | function real($v) { 120 | return new \Data\Num\Float($v); 121 | } 122 | 123 | function right($v) { 124 | return new \Data\Either\Right($v); 125 | } 126 | 127 | function str($v) { 128 | return new \Data\Str($v); 129 | } 130 | 131 | function string($v) { 132 | return new \Data\Str($v); 133 | } 134 | 135 | function true() { 136 | return new \Data\Bool\TrueClass; 137 | } 138 | 139 | function tuple() { 140 | return new \Data\Tuple(func_get_args()); 141 | } 142 | 143 | function undefined() { 144 | return new \Data\Undefined; 145 | } 146 | 147 | function void() { 148 | return new \Data\Void; 149 | } 150 | -------------------------------------------------------------------------------- /old/libs/Apache.class.php: -------------------------------------------------------------------------------- 1 | 3 | # 4 | # Permission is hereby granted, free of charge, to any person 5 | # obtaining a copy of this software and associated documentation files 6 | # (the "Software"), to deal in the Software without restriction, 7 | # including without limitation the rights to use, copy, modify, merge, 8 | # publish, distribute, sublicense, and/or sell copies of the Software, 9 | # and to permit persons to whom the Software is furnished to do so, 10 | # subject to the following conditions: 11 | # 12 | # The above copyright notice and this permission notice shall be 13 | # included in all copies or substantial of portions the Software. 14 | # 15 | # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 16 | # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 17 | # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 18 | # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE 19 | # LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 20 | # OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 21 | # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 22 | 23 | class Apache { 24 | public static function environment($variable) { 25 | return apache_getenv($variable); 26 | } 27 | 28 | public static function note($name) { 29 | return apache_note($name); 30 | } 31 | 32 | public static function set_environment($variable, $value) { 33 | return apache_setenv($variable, $value); 34 | } 35 | 36 | public static function modules() { 37 | return apache_get_modules(); 38 | } 39 | 40 | public static function version() { 41 | return apache_get_version(); 42 | } 43 | 44 | public static function lookup_uri($filename) { 45 | return apache_lookup_uri($filename); 46 | } 47 | 48 | public static function request_headers() { 49 | return apache_request_headers(); 50 | } 51 | 52 | public static function reset_timeout() { 53 | return apache_reset_timeout(); 54 | } 55 | 56 | public static function response_headers() { 57 | return apache_response_headers(); 58 | } 59 | } -------------------------------------------------------------------------------- /old/libs/PHP.class.php: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/haskellcamargo/rawr/5f49fc8bdb0096db4d09b77d6260be6f7829ef89/old/libs/PHP.class.php -------------------------------------------------------------------------------- /old/modules/Memoize.class.php: -------------------------------------------------------------------------------- 1 | 3 | # 4 | # Permission is hereby granted, free of charge, to any person 5 | # obtaining a copy of this software and associated documentation files 6 | # (the "Software"), to deal in the Software without restriction, 7 | # including without limitation the rights to use, copy, modify, merge, 8 | # publish, distribute, sublicense, and/or sell copies of the Software, 9 | # and to permit persons to whom the Software is furnished to do so, 10 | # subject to the following conditions: 11 | # 12 | # The above copyright notice and this permission notice shall be 13 | # included in all copies or substantial of portions the Software. 14 | # 15 | # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 16 | # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 17 | # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 18 | # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE 19 | # LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 20 | # OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 21 | # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 22 | 23 | # Use memoization to store already processed data in a static variable 24 | # This way you can increase the performance in more than 95% of 25 | # already processed data. 26 | 27 | # Closure → Mixed 28 | $memoize = function($func) { 29 | return function() use ($func) { 30 | static $cache = []; 31 | $args = func_get_args(); 32 | $key = md5(serialize($args)); 33 | if (!isset($cache[$key])) 34 | $cache[$key] = call_user_func_array($func, $args); 35 | return $cache[$key]; 36 | }; 37 | }; -------------------------------------------------------------------------------- /old/modules/Monad.class.php: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/haskellcamargo/rawr/5f49fc8bdb0096db4d09b77d6260be6f7829ef89/old/modules/Monad.class.php -------------------------------------------------------------------------------- /old/patternMatching/Assertion.php: -------------------------------------------------------------------------------- 1 | 3 | # 4 | # Permission is hereby granted, free of charge, to any person 5 | # obtaining a copy of this software and associated documentation files 6 | # (the "Software"), to deal in the Software without restriction, 7 | # including without limitation the rights to use, copy, modify, merge, 8 | # publish, distribute, sublicense, and/or sell copies of the Software, 9 | # and to permit persons to whom the Software is furnished to do so, 10 | # subject to the following conditions: 11 | # 12 | # The above copyright notice and this permission notice shall be 13 | # included in all copies or substantial of portions the Software. 14 | # 15 | # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 16 | # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 17 | # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 18 | # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE 19 | # LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 20 | # OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 21 | # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 22 | 23 | # This class contains only static functions and is responsible by checking and 24 | # getting information about characters and strings. 25 | class Assertion { 26 | # A character is lower when it is an alpha char and the character equals to 27 | # itself when applied to strtolower function. 28 | static function isLower($char) { # boolean isLower(string char) 29 | return ctype_alpha($char) && $char == strtolower($char); 30 | } 31 | 32 | # A character is upper when it is an alpha char and the character equals to 33 | # itself when applied to strtoupper function; 34 | static function isUpper($char) { # boolean isUpper(string char) 35 | return ctype_alpha($char) && $char == strtoupper($char); 36 | } 37 | 38 | # A character is letter when it is an alpha char. 39 | static function isLetter($char) { # boolean isLetter(string char) 40 | return ctype_alpha($char); 41 | } 42 | } 43 | -------------------------------------------------------------------------------- /old/patternMatching/Lexer.php: -------------------------------------------------------------------------------- 1 | 3 | # 4 | # Permission is hereby granted, free of charge, to any person 5 | # obtaining a copy of this software and associated documentation files 6 | # (the "Software"), to deal in the Software without restriction, 7 | # including without limitation the rights to use, copy, modify, merge, 8 | # publish, distribute, sublicense, and/or sell copies of the Software, 9 | # and to permit persons to whom the Software is furnished to do so, 10 | # subject to the following conditions: 11 | # 12 | # The above copyright notice and this permission notice shall be 13 | # included in all copies or substantial of portions the Software. 14 | # 15 | # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 16 | # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 17 | # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 18 | # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE 19 | # LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 20 | # OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 21 | # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 22 | 23 | # This class contains only static functions and is responsible by checking and 24 | # getting information about characters and strings. 25 | 26 | # This is the lexer. It is the base for the tokenizer and contains information 27 | # about EOF and also carries the values of the input, the current char and the 28 | # current position. 29 | abstract class Lexer { 30 | const EOF = -1; 31 | const EOF_TYPE = 1; 32 | protected $input 33 | , $pos = 0 34 | , $char; 35 | 36 | # Receives the input and stores it. Defines the current char as the value 0 37 | # of the input, defined in $this->pos. 38 | function __construct($input) { 39 | $this->input = $input; 40 | $this->char = $this->input[$this->pos]; 41 | } 42 | 43 | # Increments the position of the current char and when the position is 44 | # greater or equals to the size of the input, we got the end of the file, 45 | # therefore the compiler can here stop. Otherwise, we let the current char 46 | # as the next char of the input. 47 | function consume() { 48 | $this->pos++; 49 | if ($this->pos >= strlen($this->input)) 50 | $this->char = self :: EOF; 51 | else 52 | $this->char = $this->input[$this->pos]; 53 | } 54 | 55 | # Responsible for iterating the characters and returning tokens. 56 | abstract function nextToken(); 57 | # Responsible for giving readable information about the tokens. 58 | abstract function getTokenName($type); 59 | } 60 | -------------------------------------------------------------------------------- /old/patternMatching/TestLexer.php: -------------------------------------------------------------------------------- 1 | nextToken(); 10 | 11 | while ($token->type != 1) { 12 | echo $token . "\n"; 13 | $token = $lexer->nextToken(); 14 | } 15 | } 16 | } 17 | 18 | new TestLexer("Either (Error _) (Str name)"); 19 | -------------------------------------------------------------------------------- /old/patternMatching/Token.php: -------------------------------------------------------------------------------- 1 | type 7 | , $this->text ) = [$type, $text]; 8 | } 9 | 10 | function __toString() { 11 | $tname = Tokenizer :: $tokenNames[$this->type]; 12 | return "<'{$this->text}', {$tname}>"; 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /old/patternMatching/TokenBehavior.php: -------------------------------------------------------------------------------- 1 | ", "T_OBJ", "T_VAR", "T_WILDCARD", "T_LPAREN", "T_RPAREN" 12 | ]; 13 | 14 | function getTokenName($name) { 15 | return self :: $tokenNames[$name]; 16 | } 17 | 18 | function __construct($input) { 19 | parent :: __construct($input); 20 | } 21 | 22 | function nextToken() { 23 | while ($this->char != self :: EOF) { 24 | switch ($this->char) { 25 | case " ": 26 | case "\t": 27 | case "\n": 28 | case "\r": 29 | $this->whitespace(); 30 | continue; 31 | case "(": 32 | $this->consume(); 33 | return new Token(self :: T_LPAREN, "("); 34 | case ")": 35 | $this->consume(); 36 | return new Token(self :: T_RPAREN, ")"); 37 | case "_": 38 | $this->consume(); 39 | return new Token(self :: T_WILDCARD, "_"); 40 | default: 41 | if (Assertion :: isUpper($this->char)) { 42 | return $this->T_OBJ(); 43 | } 44 | else if (Assertion :: isLower($this->char)) { 45 | return $this->T_VAR(); 46 | } 47 | 48 | throw new Exception("Invalid character: " 49 | . $this->char); 50 | } 51 | } 52 | return new Token(self :: EOF_TYPE, ""); 53 | } 54 | 55 | function whitespace() { 56 | while (ctype_space($this->char)) 57 | $this->consume(); 58 | } 59 | 60 | function T_OBJ() { 61 | $acc = $this->char; 62 | $this->consume(); 63 | while (Assertion :: isLetter($this->char)) { 64 | $acc .= $this->char; 65 | $this->consume(); 66 | } 67 | return new Token(self :: T_OBJ, $acc); 68 | } 69 | 70 | function T_VAR() { 71 | $acc = $this->char; 72 | $this->consume(); 73 | while (Assertion :: isLetter($this->char)) { 74 | $acc .= $this->char; 75 | $this->consume(); 76 | } 77 | return new Token(self :: T_VAR, $acc); 78 | } 79 | } 80 | -------------------------------------------------------------------------------- /old/slap.log: -------------------------------------------------------------------------------- 1 | 2015-02-24T20:15:54.192Z - info: starting... 2 | 2015-02-24T20:42:22.089Z - info: starting... 3 | -------------------------------------------------------------------------------- /old/typeclasses/Enum.php: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/haskellcamargo/rawr/5f49fc8bdb0096db4d09b77d6260be6f7829ef89/old/typeclasses/Enum.php -------------------------------------------------------------------------------- /old/typeclasses/Eq.interface.php: -------------------------------------------------------------------------------- 1 | 3 | # 4 | # Permission is hereby granted, free of charge, to any person 5 | # obtaining a copy of this software and associated documentation files 6 | # (the "Software"), to deal in the Software without restriction, 7 | # including without limitation the rights to use, copy, modify, merge, 8 | # publish, distribute, sublicense, and/or sell copies of the Software, 9 | # and to permit persons to whom the Software is furnished to do so, 10 | # subject to the following conditions: 11 | # 12 | # The above copyright notice and this permission notice shall be 13 | # included in all copies or substantial of portions the Software. 14 | # 15 | # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 16 | # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 17 | # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 18 | # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE 19 | # LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 20 | # OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 21 | # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 22 | 23 | namespace TypeClass\Contract; 24 | 25 | interface IEq { 26 | function diff($a, $a); # :: (Eq a) => (a, a) -> bool 27 | function eq($a, $a); # :: (Eq a) => (a, a) -> bool 28 | } -------------------------------------------------------------------------------- /old/typeclasses/Eq.php: -------------------------------------------------------------------------------- 1 | 3 | # 4 | # Permission is hereby granted, free of charge, to any person 5 | # obtaining a copy of this software and associated documentation files 6 | # (the "Software"), to deal in the Software without restriction, 7 | # including without limitation the rights to use, copy, modify, merge, 8 | # publish, distribute, sublicense, and/or sell copies of the Software, 9 | # and to permit persons to whom the Software is furnished to do so, 10 | # subject to the following conditions: 11 | # 12 | # The above copyright notice and this permission notice shall be 13 | # included in all copies or substantial of portions the Software. 14 | # 15 | # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 16 | # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 17 | # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 18 | # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE 19 | # LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 20 | # OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 21 | # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 22 | 23 | # Move to other files 24 | 25 | // namespace TypeClass; 26 | 27 | 28 | // # Implement this on Rawr: http://stackoverflow.com/questions/787692/operator-overloading-in-php 29 | 30 | // interface Integral extends Eq {} 31 | 32 | // interface Real {} 33 | // interface Enum {} 34 | 35 | // interface Eq extends Ord { 36 | // function eq(&$a); # :: (a, a) -> Bool 37 | // function diff(&$a); # :: (a, a) -> Bool 38 | // } 39 | 40 | // interface Ord { 41 | // function compare(&$a); # :: (a, a) -> Ordering 42 | // function eq(&$a); # :: (a, a) -> Bool 43 | // function gt(&$a); # :: (a, a) -> Bool 44 | // function gte(&$a); # :: (a, a) -> Bool 45 | // function lt(&$a); # :: (a, a) -> Bool 46 | // function lte(&$a); # :: (a, a) -> Bool 47 | // function max(&$a); # :: (a, a) -> a 48 | // function min(&$a); # :: (a, a) -> a 49 | // } 50 | 51 | // interface Show { 52 | // function show(); # :: (Show a) => a -> a 53 | // } 54 | 55 | // define('EQ', 'EQ'); 56 | // define('GT', 'GT'); 57 | // define('LT', 'LT'); 58 | // class Ordering extends DataTypes {} 59 | 60 | namespace TypeClass; 61 | 62 | class Eq implements Contract\IEq { 63 | function diff($x, $y) { # :: (Eq a) => (a, a) -> bool 64 | return $x !== $y; 65 | } 66 | 67 | function eq($x, $y) { # :: (Eq a) => (a, a) -> bool 68 | return $x === $y; 69 | } 70 | } -------------------------------------------------------------------------------- /old/typeclasses/Ord.interface.php: -------------------------------------------------------------------------------- 1 | 3 | # 4 | # Permission is hereby granted, free of charge, to any person 5 | # obtaining a copy of this software and associated documentation files 6 | # (the "Software"), to deal in the Software without restriction, 7 | # including without limitation the rights to use, copy, modify, merge, 8 | # publish, distribute, sublicense, and/or sell copies of the Software, 9 | # and to permit persons to whom the Software is furnished to do so, 10 | # subject to the following conditions: 11 | # 12 | # The above copyright notice and this permission notice shall be 13 | # included in all copies or substantial of portions the Software. 14 | # 15 | # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 16 | # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 17 | # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 18 | # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE 19 | # LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 20 | # OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 21 | # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 22 | 23 | namespace TypeClass\Contract; 24 | 25 | interface IOrd { 26 | function GT($a, $a); # :: (Ord a) => (a, a) -> bool 27 | function LT($a, $a); # :: (Ord a) => (a, a) -> bool 28 | } -------------------------------------------------------------------------------- /old/typeclasses/Ord.php: -------------------------------------------------------------------------------- 1 | 3 | # 4 | # Permission is hereby granted, free of charge, to any person 5 | # obtaining a copy of this software and associated documentation files 6 | # (the "Software"), to deal in the Software without restriction, 7 | # including without limitation the rights to use, copy, modify, merge, 8 | # publish, distribute, sublicense, and/or sell copies of the Software, 9 | # and to permit persons to whom the Software is furnished to do so, 10 | # subject to the following conditions: 11 | # 12 | # The above copyright notice and this permission notice shall be 13 | # included in all copies or substantial of portions the Software. 14 | # 15 | # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 16 | # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 17 | # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 18 | # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE 19 | # LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 20 | # OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 21 | # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 22 | 23 | namespace TypeClass; 24 | 25 | class Ord implements Contract\IOrd { 26 | function GT($x, $y) { # :: (Ord a) => (a, a) -> bool 27 | return $x > $y; 28 | } 29 | 30 | function LT($x, $y) { # :: (Ord a) => (a, a) -> bool 31 | return $x < $y; 32 | } 33 | } -------------------------------------------------------------------------------- /old/typeclasses/Show.php: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/haskellcamargo/rawr/5f49fc8bdb0096db4d09b77d6260be6f7829ef89/old/typeclasses/Show.php -------------------------------------------------------------------------------- /old/typing/TypeInference.class.php: -------------------------------------------------------------------------------- 1 | 3 | # 4 | # Permission is hereby granted, free of charge, to any person 5 | # obtaining a copy of this software and associated documentation files 6 | # (the "Software"), to deal in the Software without restriction, 7 | # including without limitation the rights to use, copy, modify, merge, 8 | # publish, distribute, sublicense, and/or sell copies of the Software, 9 | # and to permit persons to whom the Software is furnished to do so, 10 | # subject to the following conditions: 11 | # 12 | # The above copyright notice and this permission notice shall be 13 | # included in all copies or substantial of portions the Software. 14 | # 15 | # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 16 | # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 17 | # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 18 | # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE 19 | # LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 20 | # OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 21 | # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 22 | 23 | namespace Data; 24 | 25 | class TypeInference { 26 | public static function infer($variable) { 27 | 28 | if (is_float($variable)) 29 | return real ($variable); 30 | 31 | else if (is_integer($variable)) 32 | return int ($variable); 33 | 34 | else if (is_string($variable)) 35 | return string ($variable); 36 | 37 | else if (is_array($variable)) 38 | return collection ($variable); 39 | 40 | else if (is_bool($variable)) 41 | return bool ($variable); 42 | 43 | else if (is_callable($variable)) 44 | return func ($variable); 45 | 46 | else if (is_null($variable)) 47 | return Null; 48 | 49 | else return $variable; 50 | } 51 | 52 | public function isTrue($value) { 53 | if ($value === True) return True; # Redundant, but increases performance. 54 | # Use strict-equality. 55 | if (is_object($value)) { 56 | if ($value instanceof Bool\TrueClass) return true; 57 | else if ($value instanceof Bool\FalseClass) return false; 58 | else if ($value instanceof Bool) 59 | return $value->value(); 60 | 61 | throw new \Exception("Type error: Can't determine value"); 62 | } 63 | } 64 | 65 | public static function toPrimitive($variable) { 66 | if (is_object($variable)) { 67 | if (method_exists(get_class($variable), 'value')) { 68 | return $variable->value(); 69 | } else { 70 | if ($variable instanceof TrueClass) 71 | return True; 72 | else if ($variable instanceof FalseClass) 73 | return False; 74 | else 75 | return Null; 76 | } 77 | } else { 78 | return $variable; 79 | } 80 | } 81 | 82 | public static function fromStr($str) { 83 | if (preg_match("/^([0-9]*)$/", $str)) 84 | return (int) $str; 85 | else if (preg_match("/^([0-9]*\.[0-9]*)$/", $str)) 86 | return (float) $str; 87 | } 88 | } -------------------------------------------------------------------------------- /src/Core/TypeInference.php: -------------------------------------------------------------------------------- 1 | value(); 43 | } 44 | return null; 45 | } 46 | return $var; 47 | } 48 | } 49 | } -------------------------------------------------------------------------------- /src/DataType/Bool.php: -------------------------------------------------------------------------------- 1 | value = (bool) $value; 20 | } 21 | 22 | /** 23 | * Logical and. Both values must be true. 24 | * @author Marcelo Camargo 25 | * @param Bool $b 26 | * @return Bool 27 | */ 28 | public function _and(Bool &$b) 29 | { 30 | return new Bool($this->value && $b->value); 31 | } 32 | 33 | /** 34 | * Logical or. At least one of the values must be true. 35 | * @author Marcelo Camargo 36 | * @param Bool $b 37 | * @return Bool 38 | */ 39 | public function _or(Bool &$b) 40 | { 41 | return new Bool($this->value || $b->value); 42 | } 43 | 44 | /** 45 | * Different of. Returns true if the values are not the same. 46 | * @author Marcelo Camargo 47 | * @param Bool $b 48 | * @return Bool 49 | */ 50 | public function diff(Bool &$b) 51 | { 52 | return new Bool($this->value !== $b->value); 53 | } 54 | 55 | /** 56 | * Equality. Both values must be equal. 57 | * @author Marcelo Camargo 58 | * @param Bool $b 59 | * @return Bool 60 | */ 61 | public function eq(Bool &$b) 62 | { 63 | return new Bool($this->value === $b->value); 64 | } 65 | 66 | /** 67 | * Greater than. 68 | * @author Marcelo Camargo 69 | * @param Bool $b 70 | * @return Bool 71 | */ 72 | public function gt(Bool &$b) 73 | { 74 | return new Bool($this->value > $b->value); 75 | } 76 | 77 | /** 78 | * Greater or equal. 79 | * @author Marcelo Camargo 80 | * @param Bool $b 81 | * @return Bool 82 | */ 83 | public function gtOrEq(Bool &$b) 84 | { 85 | return new Bool($this->value >= $b->value); 86 | } 87 | 88 | /** 89 | * Applies a function if value is truthy. 90 | * @author Marcelo Camargo 91 | * @param mixed $f The function to be applied (string, Closure, Fun) 92 | * @return Bool 93 | */ 94 | public function ifTrue(Fun &$f) 95 | { 96 | TypeInference::assertCallable($f); 97 | if ($this->value) { 98 | $f->invoke(); 99 | } 100 | return $this; 101 | } 102 | 103 | /** 104 | * Applies a function if value is not truthy. 105 | * @author Marcelo Camargo 106 | * @param mixed $f The function to be applied (string, Closure, Fun) 107 | * @return Bool 108 | */ 109 | public function ifFalse(Fun &$f) 110 | { 111 | TypeInference::assertCallable($f); 112 | if (!$this->value) { 113 | $f->invoke(); 114 | } 115 | return $this; 116 | } 117 | 118 | /** 119 | * Lesser than. 120 | * @author Marcelo Camargo 121 | * @param Bool $b 122 | * @return Bool 123 | */ 124 | public function lt(Bool &$b) 125 | { 126 | return new Bool($this->value < $b->value); 127 | } 128 | 129 | /** 130 | * Lesser or equal. 131 | * @author Marcelo Camargo 132 | * @param Bool $b 133 | * @return Bool 134 | */ 135 | public function ltOrEq(Bool &$b) 136 | { 137 | return new Bool($this->value <= $b->value); 138 | } 139 | 140 | /** 141 | * Negates a boolean value. 142 | * @author Marcelo Camargo 143 | * @return Bool 144 | */ 145 | public function not() 146 | { 147 | return new Bool(!$this->value); 148 | } 149 | 150 | /** 151 | * Alias to ifFalse. 152 | * @author Marcelo camargo 153 | * @param Fun $f 154 | * @return Data.Bool 155 | */ 156 | public function otherwise(Fun &$f) 157 | { 158 | return $this->ifFalse($f); 159 | } 160 | 161 | /** 162 | * Ternary method. Equivalent to `expr ? left : right`. 163 | * @author Marcelo Camargo 164 | * @param mixed $left 165 | * @param mixed $right 166 | * @return mixed 167 | */ 168 | public function thenElse($left, $right) 169 | { 170 | return $this->value 171 | ? $left 172 | : $right; 173 | } 174 | } 175 | } 176 | -------------------------------------------------------------------------------- /src/DataType/Fun.php: -------------------------------------------------------------------------------- 1 | value = $f; 35 | $this->reflObj = new ReflectionFunction($f); 36 | } 37 | 38 | /** 39 | * Exhibition when string. 40 | * @author Marcelo Camargo 41 | * @return String 42 | */ 43 | public function __toString() 44 | { 45 | $name = $this->reflObj->getName(); 46 | return "#" . ($name === "{closure}" 47 | ? "" 48 | : "<$name>"); 49 | } 50 | 51 | /** 52 | * Magic method for invocation with parenthesis. 53 | * @author Marcelo Camargo 54 | * @return Type 55 | */ 56 | public function __invoke() 57 | { 58 | $arguments = func_get_args(); 59 | 60 | if (count($arguments) > 0) { 61 | return TypeInference::determine( 62 | call_user_func_array($this->value, $arguments) 63 | ); 64 | } else { 65 | return TypeInference::determine(call_user_func($this->value)); 66 | } 67 | } 68 | 69 | /** 70 | * Returns this pointer bound to closure. 71 | * @author Marcelo Camargo 72 | * @return object 73 | */ 74 | public function boundPointer() 75 | { 76 | return $this->reflObj->getClosureThis(); 77 | } 78 | 79 | /** 80 | * Function composition, where `(f . g)(x) = f(g(x))`. 81 | * @author Marcelo Camargo 82 | * @param mixed $fn 83 | * @return Fun 84 | */ 85 | public function compose($fn) 86 | { 87 | if (!is_callable($fn)) 88 | throw new Exception; 89 | 90 | list($f, $g) = [$this->value, TypeInference::toPrimitive($fn)]; 91 | 92 | return new Fun(function($x) use($f, $g) { 93 | return $f($g($x)); 94 | }); 95 | } 96 | 97 | /** 98 | * Gets doc comment. 99 | * @author Marcelo Camargo 100 | * @return String 101 | */ 102 | public function docComment() 103 | { 104 | return new String($this->reflObj->getDocComment()); 105 | } 106 | 107 | /** 108 | * Gets end line number. 109 | * @author Marcelo Camargo 110 | * @return Int 111 | */ 112 | public function endLine() 113 | { 114 | return new Int($this->reflObj->getEndLine()); 115 | } 116 | 117 | /** 118 | * Gets extension info. 119 | * @author Marcelo Camargo 120 | * @return ReflectionExtension 121 | */ 122 | public function ext() 123 | { 124 | return $this->reflObj->getExtension(); 125 | } 126 | 127 | /** 128 | * Gets extension name. 129 | * @author Marcelo Camargo 130 | * @return String 131 | */ 132 | public function extName() 133 | { 134 | return new String($this->reflObj->getExtension); 135 | } 136 | 137 | /** 138 | * Gets file name. 139 | * @author Marcelo Camargo 140 | * @return String 141 | */ 142 | public function fileName() 143 | { 144 | return new String($this->reflObj->getFileName()); 145 | } 146 | 147 | /** 148 | * Checks if function in namespace. 149 | * @author Marcelo Camargo 150 | * @return Bool 151 | */ 152 | public function inNs() 153 | { 154 | return new Bool($this->reflObj->inNamespace()); 155 | } 156 | 157 | /** 158 | * Direct call of __invoke(). 159 | * @author Marcelo Camargo 160 | * @return Type 161 | */ 162 | public function invoke() 163 | { 164 | return $this(); 165 | } 166 | 167 | /** 168 | * Checks if closure. 169 | * @author Marcelo Camargo 170 | * @return Bool 171 | */ 172 | public function isClosure() 173 | { 174 | return new Bool($this->reflObj->isClosure()); 175 | } 176 | 177 | /** 178 | * Checks if deprecated. 179 | * @author Marcelo Camargo 180 | * @return Bool 181 | */ 182 | public function isDeprecated() 183 | { 184 | return new Bool($this->reflObj->isDeprecated()); 185 | } 186 | 187 | /** 188 | * Returns whether this function is a generator. 189 | * @author Marcelo Camargo 190 | * @return Bool 191 | */ 192 | public function isGenerator() 193 | { 194 | return new Bool($this->reflObj->isGenerator()); 195 | } 196 | 197 | /** 198 | * Checks if internal. 199 | * @author Marcelo Camargo 200 | * @return Bool 201 | */ 202 | public function isInternal() 203 | { 204 | return new Bool($this->reflObj->isInternal()); 205 | } 206 | 207 | /** 208 | * Checks if user defined. 209 | * @author Marcelo Camargo 210 | * @return Bool 211 | */ 212 | public function isUserDef() 213 | { 214 | return new Bool($this->reflObj->isUserDefined()); 215 | } 216 | 217 | /** 218 | * Checks if the function is variadic. 219 | * @author Marcelo Camargo 220 | * @return Bool 221 | */ 222 | public function isVariadic() 223 | { 224 | return new Bool($this->reflObj->isVariadic()); 225 | } 226 | 227 | /** 228 | * Gets function name. 229 | * @author Marcelo Camargo 230 | * @return String 231 | */ 232 | public function name() 233 | { 234 | return new String($this->reflObj->getName()); 235 | } 236 | 237 | /** 238 | * Gets namespace name. 239 | * @author Marcelo Camargo 240 | * @return String 241 | */ 242 | public function ns() 243 | { 244 | return new String($this->reflObj->getNamespaceName()); 245 | } 246 | 247 | /** 248 | * Gets number of parameters. 249 | * @author Marcelo Camargo 250 | * @return Int 251 | */ 252 | public function paramNum() 253 | { 254 | return new Int($this->reflObj->getNumberOfParameters()); 255 | } 256 | 257 | /** 258 | * Gets parameters. 259 | * @author Marcelo Camargo 260 | * @return Collection 261 | */ 262 | public function params() 263 | { 264 | return new Collection($this->reflObj->getParameters()); 265 | } 266 | 267 | /** 268 | * Gets number of required parameters. 269 | * @author Marcelo Camargo 270 | * @return Int 271 | */ 272 | public function reqParamNum() 273 | { 274 | return new Int($this->reflObj->getNumberOfRequiredParamters()); 275 | } 276 | 277 | /** 278 | * Checks whether the function returns a reference. 279 | * @author Marcelo Camargo 280 | * @return Bool 281 | */ 282 | public function returnsRef() 283 | { 284 | return new Bool($this->reflObj->returnsReference()); 285 | } 286 | 287 | /** 288 | * Gets function short name. 289 | * @author Marcelo Camargo 290 | * @return String 291 | */ 292 | public function shortName() 293 | { 294 | return new String($this->reflObj->getShortName()); 295 | } 296 | 297 | /** 298 | * Gets starting line number. 299 | * @author Marcelo Camargo 300 | * @return Int 301 | */ 302 | public function startLine() 303 | { 304 | return new Int($this->reflObj->getStartLine()); 305 | } 306 | 307 | /** 308 | * @author Marcelo Camargo 309 | * @return Dictionary 310 | */ 311 | public function staticVariables() 312 | { 313 | return new Dictionary($this->reflObj->getStaticVariables()); 314 | } 315 | 316 | /** 317 | * Returns the scoope associated to the closure. 318 | * @author Marcelo Camargo 319 | * @return ReflectionClass 320 | */ 321 | public function scope() 322 | { 323 | return $this->reflObj->getClosureScopeClass(); 324 | } 325 | } 326 | } -------------------------------------------------------------------------------- /src/DataType/Maybe.Just.php: -------------------------------------------------------------------------------- 1 | value = $value; 14 | } 15 | 16 | public function bind($fn) 17 | { 18 | TypeInference::assertCallable($fn); 19 | return $fn($this->value); 20 | } 21 | 22 | public function fromJust() 23 | { 24 | return $this->value; 25 | } 26 | 27 | public function fromMaybe($_) 28 | { 29 | return $this->value; 30 | } 31 | 32 | public function isJust() 33 | { 34 | return new Bool(true); 35 | } 36 | 37 | public function isNothing() 38 | { 39 | return new Bool(false); 40 | } 41 | 42 | public function maybe($_, $fn) 43 | { 44 | return $fn($this->value); 45 | } 46 | 47 | public function toList() 48 | { 49 | return new Collection([$this->value]); 50 | } 51 | } 52 | } -------------------------------------------------------------------------------- /src/DataType/Maybe.Nothing.php: -------------------------------------------------------------------------------- 1 | >= operator. Applies the received function 12 | * if no errors happen. 13 | */ 14 | abstract function bind($fn); 15 | 16 | /** 17 | * Tries to extract an element out of a Just. 18 | */ 19 | abstract function fromJust(); 20 | 21 | /** 22 | * Takes a default value. If this inner value is Nothing, returns the 23 | * default value, otherwise returns this value extracted. 24 | */ 25 | abstract function fromMaybe($def); 26 | 27 | /** 28 | * Checks if the element is a Just. 29 | */ 30 | abstract function isJust(); 31 | 32 | /** 33 | * Checks if the element is a Nothing. 34 | */ 35 | abstract function isNothing(); 36 | 37 | /** 38 | * returns the default value or applies the received function. 39 | */ 40 | abstract function maybe($def, $fn); 41 | 42 | /** 43 | * Returns an empty list when Nothing and a singleton list when Just. 44 | */ 45 | abstract function toList(); 46 | } 47 | 48 | function Maybe($value) { 49 | return is_null($value) 50 | ? new Nothing 51 | : new Just($value); 52 | } 53 | } -------------------------------------------------------------------------------- /src/DataType/Type.php: -------------------------------------------------------------------------------- 1 | value; 12 | } 13 | } 14 | } -------------------------------------------------------------------------------- /tests/Main.php: -------------------------------------------------------------------------------- 1 | a = new Bool(true); 20 | $this->b = new Bool(true); 21 | $this->c = new Bool(false); 22 | } 23 | 24 | public function testAnd() 25 | { 26 | $result = [ 27 | 0 => (new Bool(true))->_and(new Bool(true)), 28 | 1 => (new Bool(false))->_and(new Bool(true)) 29 | ]; 30 | 31 | $this->assertEquals(new Bool(true), $result[0]); 32 | $this->assertEquals(new Bool(false), $result[1]); 33 | } 34 | 35 | public function testOr() 36 | { 37 | $result = [ 38 | 0 => (new Bool(true))->_or(new Bool(true)), 39 | 1 => (new Bool(false))->_or(new Bool(true)), 40 | 2 => (new Bool(false))->_or(new Bool(false)) 41 | ]; 42 | 43 | $this->assertEquals(new Bool(true), $result[0]); 44 | $this->assertEquals(new Bool(true), $result[1]); 45 | $this->assertEquals(new Bool(false), $result[2]); 46 | } 47 | 48 | public function testDiff() 49 | { 50 | $result = [ 51 | 0 => (new Bool(true))->diff(new Bool(true)), 52 | 1 => (new Bool(false))->diff(new Bool(true)) 53 | ]; 54 | 55 | $this->assertEquals(new Bool(false), $result[0]); 56 | $this->assertEquals(new Bool(true), $result[1]); 57 | } 58 | 59 | public function testEq() 60 | { 61 | $result = [ 62 | 0 => (new Bool(true))->eq(new Bool(true)), 63 | 1 => (new Bool(false))->eq(new Bool(true)) 64 | ]; 65 | 66 | $this->assertEquals(new Bool(true), $result[0]); 67 | $this->assertEquals(new Bool(false), $result[1]); 68 | } 69 | 70 | public function testGt() 71 | { 72 | $result = [ 73 | 0 => (new Bool(true))->gt(new Bool(true)), 74 | 1 => (new Bool(false))->gt(new Bool(true)) 75 | ]; 76 | 77 | $this->assertEquals(new Bool(false), $result[0]); 78 | $this->assertEquals(new Bool(false), $result[1]); 79 | } 80 | 81 | public function testGtOrEq() 82 | { 83 | $result = [ 84 | 0 => (new Bool(true))->gtOrEq(new Bool(true)), 85 | 1 => (new Bool(false))->gtOrEq(new Bool(true)) 86 | ]; 87 | 88 | $this->assertEquals(new Bool(true), $result[0]); 89 | $this->assertEquals(new Bool(false), $result[1]); 90 | } 91 | 92 | public function testIfTrue() 93 | { 94 | $result = (new Bool(true))->ifTrue(new Fun(function() {})); 95 | 96 | $this->assertEquals(new Bool(true), $result); 97 | } 98 | 99 | public function testIfFalse() 100 | { 101 | $result = (new Bool(false))->ifFalse(new Fun(function() {})); 102 | 103 | $this->assertEquals(new Bool(false), $result); 104 | } 105 | 106 | public function testLt() 107 | { 108 | $result = [ 109 | 0 => (new Bool(true))->lt(new Bool(true)), 110 | 1 => (new Bool(false))->lt(new Bool(true)) 111 | ]; 112 | 113 | $this->assertEquals(new Bool(false), $result[0]); 114 | $this->assertEquals(new Bool(true), $result[1]); 115 | } 116 | 117 | public function testLtOrEq() 118 | { 119 | $result = [ 120 | 0 => (new Bool(true))->ltOrEq(new Bool(true)), 121 | 1 => (new Bool(false))->ltOrEq(new Bool(true)) 122 | ]; 123 | 124 | $this->assertEquals(new Bool(true), $result[0]); 125 | $this->assertEquals(new Bool(true), $result[1]); 126 | } 127 | 128 | public function testNot() 129 | { 130 | $result = [ 131 | 0 => (new Bool(true))->not(), 132 | 1 => (new Bool(false))->not() 133 | ]; 134 | 135 | $this->assertEquals(new Bool(false), $result[0]); 136 | $this->assertEquals(new Bool(true), $result[1]); 137 | } 138 | 139 | public function testOtherwise() 140 | { 141 | $result = (new Bool(false))->otherwise(new Fun(function() {})); 142 | 143 | $this->assertEquals(new Bool(false), $result); 144 | } 145 | 146 | public function testThenElse() 147 | { 148 | $result = (new Bool(true))->thenElse(1, 2); 149 | 150 | $this->assertEquals(1, $result); 151 | } 152 | } -------------------------------------------------------------------------------- /tests/TestFun.php: -------------------------------------------------------------------------------- 1 | assertEquals(new Bool(true), $a()); 34 | } 35 | 36 | public function testInvokeAlias() 37 | { 38 | $a = new Fun(function() { 39 | return new Bool(true); 40 | }); 41 | 42 | $this->assertEquals(new Bool(true), $a->invoke()); 43 | } 44 | } --------------------------------------------------------------------------------