hask.lang.syntax
– The “syntactic” elements to build Hask programs¶
-
class
hask.lang.syntax.
sig
(signature)[source]¶ Decorator to convert a Python function into a statically typed function (
TypedFunc
object).TypedFuncs are automagically curried, and polymorphic type arguments will be inferred by the type system.
Usage:
@sig(H/ int >> int >> int ) def add(x, y): return x + y @sig(H[(Show, "a")]/ >> "a" >> str) def to_str(x): return str(x)
-
H
-
class
hask.lang.syntax.
__constraints__
(constraints=None)[source]¶ H/ creates a new function type signature.
Examples:
(H/ int >> int >> int) (H/ (H/ "a" >> "b" >> "c") >> "b" >> "a" >> "c") (H/ func >> set >> set) (H/ (H/ "a" >> "b") >> ["a"] >> ["b"]) (H[(Eq, "a")]/ "a" >> ["a"] >> bool) (H/ int >> int >> t(Maybe, int)) (H/ int >> None)
See
sig
for more information on type signature decorators.
-
class
hask.lang.syntax.
Syntax
(err_msg)[source]¶ Base class for new syntactic constructs. All of the new “syntax” elements of Hask inherit from this class.
By default, a piece of syntax will raise a syntax error with a standard error message if the syntax object is used with a Python builtin operator.
Subclasses may override these methods to define what syntax is valid for those objects.
-
class
hask.lang.syntax.
instance
(typecls, cls)[source]¶ Special syntax for defining typeclass instances.
Example usage:
instance(Functor, Maybe).where( fmap = ... )
-
class
hask.lang.syntax.
caseof
(value)[source]¶ Pattern matching can be used to deconstruct lists and ADTs, and is a very useful control flow tool.
Usage:
~(caseof(value_to_match) | m(pattern_1) >> return_value_1 | m(pattern_2) >> return_value_2 | m(pattern_3) >> return_value_3)
Example usage:
@sig(H/ int >> int) def fib(x): return ~(caseof(x) | m(0) >> 1 | m(1) >> 1 | m(m.n) >> fib(p.n - 1) + fib(p.n - 2))
-
data
-
class
hask.lang.syntax.
__data__
[source]¶ data is part of Hask’s special syntax for defining ADTs.
Example usage:
>>> from hask import data, d, deriving, Read, Show, Eq, Ord
>>> Maybe, Nothing, Just =\ ... data.Maybe("a") == d.Nothing | d.Just("a") & \ ... deriving(Read, Show, Eq, Ord)
-
d
-
class
hask.lang.syntax.
__d__
[source]¶ d is part of hask’s special syntax for defining algebraic data types.
See
data
for more information.
-
class
hask.lang.syntax.
deriving
(*tclasses)[source]¶ deriving is part of hask’s special syntax for defining algebraic data types.
See
data
for more information.
-
hask.lang.syntax.
t
(type_constructor, *params)[source]¶ Helper to instantiate
TypeSignatureHKT
.
-
hask.lang.syntax.
typify
(fn, hkt=None)[source]¶ Convert an untyped Python function to a TypeFunc.
Parameters: - fn – The function to wrap
- hkt – A higher-kinded type wrapped in a closure (e.g.,
lambda x: t(Maybe, x)
)
Returns: A
TypedFunc
object with a polymorphic type (e.g.a -> b -> c
, etc) with the same number of arguments as fn. If hkt is supplied, the return type will be the supplied HKT parameterized by a type variable.Example usage:
@typify(hkt=lambda x: t(Maybe, x)) def add(x, y): return x + y
-
undefined
-
class
hask.lang.syntax.
__undefined__
[source]¶ Undefined value with special syntactic powers. Whenever you try to use one if its magic methods, it returns undefined. Used to prevent overzealous evaluation in pattern matching.
Its type unifies with any other type.
-
class
hask.lang.syntax.
guard
(value)[source]¶ Special syntax for guard expression.
Usage:
~(guard(<expr to test>) | c(<test_fn_1>) >> <return_value_1> | c(<test_fn_2>) >> <return_value_2> | otherwise >> <return_value_3> )
Examples:
~(guard(8) | c(lambda x: x < 5) >> "less than 5" | c(lambda x: x < 9) >> "less than 9" | otherwise >> "unsure" ) # Using guards with sections. See help(__) for information on sections. ~(guard(20) | c(__ > 10) >> 20 | c(__ == 10) >> 10 | c(__ > 5) >> 5 | otherwise >> 0)
Parameters: value – the value being tested in the guard expression Returns: the return value corresponding to the first matching condition Raises: NoGuardMatchException (if no match is found)
-
c
-
class
hask.lang.syntax.
__guard_test__
(fn)[source]¶ A case in a guard.
c
creates a new condition that can be used in a guard expression.otherwise
is a guard condition that always evaluates to True.Usage:
~(guard(<expr to test>) | c(<test_fn_1>) >> <return_value_1> | c(<test_fn_2>) >> <return_value_2> | otherwise >> <return_value_3> )
See
guard
for more details.
-
otherwise
A
c
object that evaluates to True.
-
p
-
class
hask.lang.syntax.
__var_access__
(err_msg)[source]¶ p.*
accesses a local variable bound during pattern matching.For example usage, see
caseof
.
-
m
-
class
hask.lang.syntax.
__var_bind__
(err_msg)[source]¶ m.*
binds a local variable while pattern matching.For example usage, see
caseof
.
-
hask.lang.syntax.
_t
(obj)[source]¶ Returns a string representing the type of an object, including higher-kinded types and ADTs.
Equivalent to
:t
in Haskell. Meant to be used in the REPL, but might also be useful for debugging.Parameters: obj – the object to inspect Returns: A string representation of the type Usage:
>>> from hask import _t
>>> _t(1) 'int'
>>> _t(Just("hello world")) '(Maybe str)'
-
hask.lang.syntax.
_i
(obj)[source]¶ Show information about an object.
Equivalent to
:i
in Haskell orhelp(obj)
in Python. Should only be used in the REPL.Parameters: obj – the object to inspect Usage:
>>> _i(Just("hello world")) ... >>> _i(Either) ...
-
__
-
class
hask.lang.syntax.
__section__
(syntax_err_msg)[source]¶ The class of the
__
object.This is Hask’s special syntax for operator sections: a placeholder for arguments (operands).
Example usage:
>>> from __future__ import division >>> from hask.lang import __
>>> (__+1)(5) 6
>>> (6//__) * (__-1) % 4 2
>>> (__**__)(2, 10) 1024
Operators supported:
+ - * / // ** >> << | & ^ == != > >= < <=