├── .gitignore ├── LICENSE ├── README.md ├── examples ├── example.expected.txt ├── example.idr ├── example.py ├── np.expected.txt ├── np.idr ├── np.py ├── pythag.expected.txt ├── pythag.idr └── pythag.py ├── idris-python.cabal ├── lib ├── Python.idr ├── Python │ ├── Exceptions.idr │ ├── Fields.idr │ ├── Functions.idr │ ├── IO.idr │ ├── Lib │ │ ├── BeautifulSoup.idr │ │ ├── Numpy.idr │ │ ├── Numpy │ │ │ └── Matrix.idr │ │ ├── Os.idr │ │ ├── Queue.idr │ │ ├── Requests.idr │ │ └── Threading.idr │ ├── Objects.idr │ ├── Prim.idr │ └── Telescope.idr └── python.ipkg ├── run-tests.sh ├── src ├── IRTS │ └── CodegenPython.hs ├── Main.hs └── Util │ └── PrettyPrint.hs └── stack.yaml /.gitignore: -------------------------------------------------------------------------------- 1 | cabal.sandbox.config 2 | dist/ 3 | *.ibc 4 | *.pyc 5 | .stack-work 6 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Copyright (c) 2015, idris-python contributors. 2 | (https://github.com/ziman/idris-py/graphs/contributors) 3 | All rights reserved. 4 | 5 | Redistribution and use in source and binary forms, with or without 6 | modification, are permitted provided that the following conditions are met: 7 | 8 | 1. Redistributions of source code must retain the above copyright notice, this 9 | list of conditions and the following disclaimer. 10 | 11 | 2. Redistributions in binary form must reproduce the above copyright notice, 12 | this list of conditions and the following disclaimer in the documentation 13 | and/or other materials provided with the distribution. 14 | 15 | 3. Neither the name of the copyright holder nor the names of its contributors 16 | may be used to endorse or promote products derived from this software 17 | without specific prior written permission. 18 | 19 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 20 | ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 21 | WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 22 | DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE 23 | FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 24 | DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 25 | SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 26 | CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 27 | OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 28 | OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 29 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Python back-end for Idris 2 | 3 | ## Goodies 4 | 5 | * expressions in tail positions return directly 6 | * if-elif sequences optimised 7 | - not doing comparison if it's clear which branch to take 8 | - single-branch trees turned into assertions (ignore them with `python -O`) and flattened 9 | * tail-call optimisation (does not work for mutual recursion) 10 | - There is a [full TCO branch](https://github.com/ziman/idris-py/tree/trampoline-tco) 11 | using trampolines but it consumes more stack frames, thus making non-tail-recursive 12 | programs crash earlier so it's not merged into master. 13 | - [Another full TCO branch](https://github.com/ziman/idris-py/tree/inline-tco) preserves 14 | the number of stack frames but it's even slower. 15 | * principled codegen monad makes it easy to compile from `DExp` 16 | * allows typechecked use of Python libraries 17 | ([example](https://github.com/ziman/idris-py/blob/master/examples/example.idr)) 18 | - thanks to signatures for Python objects 19 | ([example](https://github.com/ziman/idris-py/blob/master/lib/Python/Lib/BeautifulSoup.idr)). 20 | - fully dependent type signatures are supported 21 | ([example](https://github.com/ziman/idris-py/blob/master/lib/Python/Lib/Queue.idr#L18)), 22 | including arguments with defaults 23 | * allows duck typing ([example](https://github.com/ziman/idris-py/blob/master/lib/Python/Functions.idr#L30)) 24 | * error reflection yields messages like: 25 | - `Field "gets" does not exist in object signature "Session"` 26 | - `Iterable Int is not mixed into signature "Response"` 27 | * `foreach` -- higher-order FFI :) 28 | * big case trees compiled specially 29 | - constructor-cases to binary search on tags 30 | - seems to bring down `pythag 100` from 5.5 secs to 3.5 secs, probably because of `APPLY0` 31 | - constant-cases to dictionary lookups 32 | - a bit wasteful (evaluates too much in non-trivial cases) -- but apparently easy to do 33 | * comments in the generated Python code show the meaning of low-level code 34 | - constructor names next to numeric constructor tags 35 | - readable names next to mangled names 36 | * exceptions (no hierarchy yet, though) ([example](https://github.com/ziman/idris-py/blob/master/examples/example.idr#L80)) 37 | * threading, message passing and `forkPIO` ([example](https://github.com/ziman/idris-py/blob/master/examples/example.idr#L62)) 38 | * `Just x` compiles to `x`, `Nothing` compiles to `None` 39 | - this gives choice to FFI authors to say whether they care about `None` 40 | by having FFI functions take/return either bare values or maybe-values. 41 | * calling Idris from Python 42 | ([exports](https://github.com/ziman/idris-py/blob/master/examples/example.idr#L105), 43 | [usage](#calling-idris-from-python)) 44 | 45 | ## Observations 46 | 47 | * it turns out that using `Text` to represent generated code in the prettyprinter 48 | ([branches/text](https://github.com/ziman/idris-py/tree/text)) is not that much win 49 | - strict `Text` seems to be a bit slower than `String` 50 | - lazy `Text` seems to be about as fast as `String` 51 | - `String` is the simplest 52 | 53 | ### Install using Stack 54 | 55 | First, the codegen: 56 | ```bash 57 | $ stack build 58 | ``` 59 | 60 | Then, the library: 61 | ```bash 62 | $ cd lib 63 | $ stack exec idris -- --install python.ipkg 64 | ``` 65 | 66 | Some Python libraries for the example programs: 67 | ```bash 68 | $ pip install requests bs4 numpy 69 | ``` 70 | 71 | #### Running the Examples 72 | 73 | Compile the example 74 | ```bash 75 | $ cd examples/ 76 | $ stack exec idris -- example.idr -p python --codegen python -o example.py 77 | ``` 78 | 79 | ### Install using Cabal 80 | 81 | First, the codegen: 82 | ```bash 83 | $ cabal sandbox init --sandbox $IDRIS_PATH/.cabal-sandbox 84 | $ cabal configure && cabal build 85 | ``` 86 | 87 | Then, the library: 88 | ```bash 89 | $ cd lib 90 | $ idris --install python.ipkg 91 | ``` 92 | 93 | Some Python libraries for the example programs: 94 | ```bash 95 | $ pip install requests bs4 numpy 96 | ``` 97 | 98 | Finally, set up your path appropriately: 99 | ```bash 100 | $ export PATH="$PATH:$IDRIS_PATH/.cabal-sandbox/bin/" 101 | ``` 102 | 103 | #### Running the Examples 104 | 105 | Compile the example 106 | ```bash 107 | $ cd examples/ 108 | $ idris example.idr -p python --codegen python -o example.py 109 | ``` 110 | 111 | ### Calling Python from Idris 112 | ```bash 113 | $ python example.py 114 | Idris has got the following exciting features: 115 | 1. Full dependent types with dependent pattern matching 116 | 2. Simple foreign function interface (to C) 117 | 3. Compiler-supported interactive editing: the compiler helps you write code using the types 118 | 4. where clauses, with rule, simple case expressions, pattern matching let and lambda bindings 119 | 5. Dependent records with projection and update 120 | 6. Type classes 121 | 7. Type-driven overloading resolution 122 | 8. do notation and idiom brackets 123 | 9. Indentation significant syntax 124 | 10. Extensible syntax 125 | 11. Cumulative universes 126 | 12. Totality checking 127 | 13. Hugs style interactive environment 128 | Total number of features: 13 129 | 130 | thread A starting 131 | thread B starting 132 | thread A done 133 | thread B done 134 | thread A says 9121 135 | thread B says 9121 136 | 137 | And now, let's fail! 138 | -> (1) everything's fine: [Errno 13] Permission denied: '/root/hello' 139 | -> (2) everything's fine: [Errno 13] Permission denied: '/root/hello' 140 | ``` 141 | 142 | ### Calling Idris from Python 143 | 144 | ```Python 145 | >>> import example 146 | >>> example.greet() 147 | Hello world! 148 | ``` 149 | -------------------------------------------------------------------------------- /examples/example.expected.txt: -------------------------------------------------------------------------------- 1 | Idris has got the following exciting features: 2 | 1. Full dependent types with dependent pattern matching 3 | 2. Simple foreign function interface (to C) 4 | 3. Compiler-supported interactive editing: the compiler helps you write code using the types 5 | 4. where clauses, with rule, simple case expressions, pattern matching let and lambda bindings 6 | 5. Dependent records with projection and update 7 | 6. Interfaces (similar to type classes in Haskell) 8 | 7. Type-driven overloading resolution 9 | 8. do notation and idiom brackets 10 | 9. Indentation significant syntax 11 | 10. Extensible syntax 12 | 11. Cumulative universes 13 | 12. Totality checking 14 | 13. Hugs style interactive environment 15 | Total number of features: 13 16 | 17 | thread A starting 18 | thread B starting 19 | thread A or B done 20 | thread A or B done 21 | thread A says 11024 22 | thread B says 11024 23 | 24 | And now, let's fail! 25 | -> (1) everything's fine: [Errno 13] Permission denied: '/root/hello' 26 | -> (2) everything's fine: [Errno 13] Permission denied: '/root/hello' 27 | -------------------------------------------------------------------------------- /examples/example.idr: -------------------------------------------------------------------------------- 1 | module Main 2 | 3 | import Python 4 | import Python.Prim 5 | import Python.Exceptions 6 | 7 | -- These modules contain signatures for Python libraries. 8 | import Python.Lib.Os 9 | import Python.Lib.Requests 10 | import Python.Lib.BeautifulSoup 11 | import Python.Lib.Queue 12 | import Python.Lib.Threading 13 | 14 | %default total 15 | 16 | -- Even though field names are strings, 17 | -- everything is typechecked according to the signatures imported above. 18 | 19 | partial 20 | main : PIO () 21 | main = do 22 | reqs <- Requests.import_ 23 | 24 | -- (/) extracts the named attribute 25 | -- ($) calls a function 26 | -- (/.) and ($.) work with pure LHS 27 | -- (/:) and ($:) work with monadic LHS (useful for chaining) 28 | -- 29 | -- equivalent to: session = reqs.Session() 30 | session <- reqs /. "Session" $. [] 31 | 32 | -- equivalent to: html = session.get("http://idris-lang.org").text 33 | -- Notice that chaining is not a problem. 34 | html <- session /. "get" $. ["http://idris-lang.org"] /: "text" 35 | 36 | -- import Beautiful Soup 37 | bs4 <- BeautifulSoup.import_ 38 | 39 | -- construct soup from HTML 40 | soup <- bs4 /. "BeautifulSoup" $. [html, Parsers.HTML] 41 | 42 | -- get the iterator over
  • elements, given by CSS selector 43 | features <- soup /. "select" $. ["div.entry-content li"] 44 | 45 | -- print all
  • elements as features 46 | putStrLn' $ "Idris has got the following exciting features:" 47 | count <- iterate features 0 $ \i : Int, li : Obj Element => do 48 | -- collect : Iterator a -> PIO (List a) 49 | line <- concat <$> collect (li /. "strings") 50 | putStrLn' $ show (i+1) ++ ". " ++ line 51 | pure $ i + 1 52 | 53 | putStrLn' $ "Total number of features: " ++ show count 54 | putStrLn' "" 55 | 56 | 57 | -- ### Concurrency ### 58 | 59 | let thread : (String -> PIO Nat) = \name => do 60 | putStrLn' $ "thread " ++ name ++ " starting" 61 | html <- session /. "get" $. ["http://idris-lang.org"] /: "text" 62 | putStrLn' $ "thread " ++ name ++ " done" 63 | pure $ length html 64 | 65 | thrA <- forkPIO $ thread "A" 66 | thrB <- forkPIO $ thread "B" 67 | resA <- wait thrA 68 | resB <- wait thrB 69 | 70 | putStrLn' $ "thread A says " ++ show resA 71 | putStrLn' $ "thread B says " ++ show resB 72 | putStrLn' "" 73 | 74 | 75 | -- ### Exceptions ### 76 | 77 | os <- Os.import_ 78 | putStrLn' "And now, let's fail!" 79 | 80 | -- the standard try-catch variant 81 | try (do 82 | os /. "mkdir" $. ["/root/hello"] 83 | putStrLn' $ "Something's wrong, your root's homedir is writable!" 84 | ) `catch` (\etype, e => case etype of 85 | OSError => putStrLn' $ " -> (1) everything's fine: " ++ showException e 86 | _ => raise e 87 | ) 88 | 89 | -- Idris sugar, originally used in Effects 90 | OK ret <- try $ os /. "mkdir" $. ["/root/hello"] 91 | | Except OSError e => putStrLn' (" -> (2) everything's fine: " ++ showException e) 92 | | Except _ e => raise e 93 | putStrLn' $ "Your root could probably use some security lessons!" 94 | 95 | exports : FFI_Export FFI_Py "example.py" [] 96 | exports = 97 | Fun greet "greet" $ 98 | End 99 | where 100 | greet : String -> PIO () 101 | greet name = putStrLn' $ "Hello " ++ name ++ "!" 102 | -------------------------------------------------------------------------------- /examples/np.expected.txt: -------------------------------------------------------------------------------- 1 | Just [True, False] 2 | [[ 1. -2.1 3.3 -0.1] 3 | [ 3.5 7. 0. -5.2] 4 | [ 0.5 7.2 -1.1 0. ]] 5 | [[ 1. -2.1] 6 | [ 3.3 -0.1] 7 | [ 3.5 7. ] 8 | [ 0. -5.2] 9 | [ 0.5 7.2] 10 | [-1.1 0. ]] 11 | [[ 1. -2.1 3.3] 12 | [-0.1 3.5 7. ] 13 | [ 0. -5.2 0.5] 14 | [ 7.2 -1.1 0. ]] 15 | [[ 0.49 -26.5 -9.75] 16 | [-34.64 22.87 60.55] 17 | [ -0.22 29.87 51.5 ]] 18 | [[ 16.31 -10.68 -18.25] 19 | [-10.68 88.29 52.15] 20 | [-18.25 52.15 53.3 ]] 21 | [[ 13.5 26. 2.75 -18.3 ] 22 | [ 26. 105.25 -14.85 -36.19] 23 | [ 2.75 -14.85 12.1 -0.33] 24 | [ -18.3 -36.19 -0.33 27.05]] 25 | [[ -9.2 31.5 -35.7 6.9 ] 26 | [ 14.12 150.42 -65.4 -45.44] 27 | [ -0.46 -15.86 -0.9 47.72] 28 | [ 21.84 -36.18 54.28 10.2 ]] 29 | -------------------------------------------------------------------------------- /examples/np.idr: -------------------------------------------------------------------------------- 1 | import Data.Vect 2 | import Python 3 | import Python.Lib.Numpy.Matrix 4 | 5 | %default total 6 | 7 | f : Nat -> Maybe (List Bool) 8 | f Z = Just [True, False] 9 | f (S n) = f n 10 | 11 | xs : Matrix 3 4 DDouble 12 | xs = array _ 13 | [[1.0,-2.1, 3.3, -0.1] 14 | ,[3.5, 7.0, 0.0, -5.2] 15 | ,[0.5, 7.2,-1.1, 0.0] 16 | ] 17 | 18 | ys : Matrix 6 2 DDouble 19 | ys = reshape xs 20 | 21 | zs : Matrix 4 3 DDouble 22 | zs = reshape ys 23 | 24 | main : PIO () 25 | main = do 26 | printLn' $ f 4 27 | printLn' xs 28 | printLn' ys 29 | printLn' zs 30 | -- printLn (ys `dot` zs) -- won't work! 31 | printLn' (xs `dot` zs) 32 | printLn' (xs `dot` transpose xs) 33 | printLn' (transpose xs `dot` xs) 34 | printLn' $ 2 * transpose (zs `dot` xs) + fromDouble 0.2 35 | -------------------------------------------------------------------------------- /examples/np.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | 3 | import sys 4 | import importlib 5 | import math 6 | 7 | Unit = object() 8 | World = object() 9 | 10 | class IdrisError(Exception): 11 | pass 12 | 13 | def _idris_error(msg): 14 | raise IdrisError(msg) 15 | 16 | def _idris_pymodule(name): 17 | return importlib.import_module(name) 18 | 19 | def _idris_call(f, args): 20 | return f(*list(args)) 21 | 22 | def _idris_foreach(it, st, f): 23 | for x in it: 24 | # Apply st, x, world 25 | st = APPLY0(APPLY0(APPLY0(f, st), x), World) 26 | return st 27 | 28 | def _idris_try(f, fail, succ): 29 | try: 30 | result = APPLY0(f, World) # apply to world 31 | return APPLY0(succ, result) 32 | except Exception as e: 33 | return APPLY0(fail, e) 34 | 35 | def _idris_raise(e): 36 | raise e 37 | 38 | def _idris_marshal_PIO(action): 39 | return lambda: APPLY0(action, World) # delayed apply-to-world 40 | 41 | def _idris_get_global(name): 42 | return globals()[name] 43 | 44 | class _ConsIter(object): 45 | def __init__(self, node): 46 | self.node = node 47 | 48 | def next(self): 49 | if self.node.isNil: 50 | raise StopIteration 51 | else: 52 | result = self.node.head 53 | self.node = self.node.tail 54 | return result 55 | 56 | class ConsList(object): 57 | def __init__(self, isNil=True, head=None, tail=None): 58 | self.isNil = isNil 59 | self.head = head 60 | self.tail = tail 61 | 62 | def __nonzero__(self): 63 | return not self.isNil 64 | 65 | def __len__(self): 66 | cnt = 0 67 | while not self.isNil: 68 | self = self.tail 69 | cnt += 1 70 | return cnt 71 | 72 | def cons(self, x): 73 | return ConsList(isNil=False, head=x, tail=self) 74 | 75 | def __iter__(self): 76 | return _ConsIter(self) 77 | 78 | # Python.Functions.$. 79 | def _idris_Python_46_Functions_46__36__46_(e0, e1, e2, e3, e4, e5): 80 | while True: 81 | return _idris_PE_95__60__36__62__95_cc6adb39( 82 | None, 83 | None, 84 | (65680, None), # {U_Python.IO.unRaw1} 85 | (65679, e3, e2, e5) # {U_Python.Functions.{$.0}1} 86 | ) 87 | 88 | # Prelude.Basics.. 89 | def _idris_Prelude_46_Basics_46__46_(e0, e1, e2, e3, e4, _idris_x): 90 | while True: 91 | return APPLY0(e3, APPLY0(e4, _idris_x)) 92 | 93 | # Python.Fields./. 94 | def _idris_Python_46_Fields_46__47__46_(e0, e1, e2, e3, e4): 95 | while True: 96 | return _idris_unsafePerformIO(None, None, (65677, e2, e3)) # {U_Python.Fields.{/.0}1} 97 | 98 | # Python.Fields.//. 99 | def _idris_Python_46_Fields_46__47__47__46_(e0, e1, e2, e3, e4, e5): 100 | while True: 101 | return _idris_unsafePerformIO(None, None, (65678, e3, e4)) # {U_Python.Fields.{//.0}1} 102 | 103 | # Force 104 | def _idris_Force(e0, e1, e2): 105 | while True: 106 | return EVAL0(e2) 107 | 108 | # PE_<$>_cc6adb39 109 | def _idris_PE_95__60__36__62__95_cc6adb39(e0, e1, e2, e3): 110 | while True: 111 | return _idris_PE_95_map_95_1b3102a6(None, None, e2, e3) 112 | 113 | # PE_Prelude.Show.List a implementation of Prelude.Show.Show, method showPrec_32c264fb 114 | def _idris_PE_95_Prelude_46_Show_46_List_32_a_32_implementation_32_of_32_Prelude_46_Show_46_Show_44__32_method_32_showPrec_95_32c264fb( 115 | e0, e1 116 | ): 117 | while True: 118 | return _idris_PE_95_Prelude_46_Show_46_List_32_a_32_implementation_32_of_32_Prelude_46_Show_46_Show_44__32_method_32_show_95_d2fe2214( 119 | e1 120 | ) 121 | 122 | # PE_Prelude.Show.List a implementation of Prelude.Show.Show, method show_d2fe2214 123 | def _idris_PE_95_Prelude_46_Show_46_List_32_a_32_implementation_32_of_32_Prelude_46_Show_46_Show_44__32_method_32_show_95_d2fe2214( 124 | e0 125 | ): 126 | while True: 127 | return (u'[' + (_idris_Prelude_46_Show_46_Prelude_46_Show_46__64_Prelude_46_Show_46_Show_36_List_32_a_58__33_show_58_0_58_show_39__58_0( 128 | None, 129 | None, 130 | (0, (65709,), (65711,)), # constructor of Prelude.Show.Show, {U_{PE_Prelude.Show.List a implementation of Prelude.Show.Show, method show_d2fe22140}1}, {U_{PE_Prelude.Show.List a implementation of Prelude.Show.Show, method show_d2fe22142}1} 131 | u'', 132 | e0 133 | ) + u']')) 134 | 135 | # PE_map_1b3102a6 136 | def _idris_PE_95_map_95_1b3102a6(e0, e1, e2, e3): 137 | while True: 138 | return _idris_Prelude_46_Functor_46_Prelude_46_Monad_46__64_Prelude_46_Functor_46_Functor_36_IO_39__32_ffi_58__33_map_58_0( 139 | None, None, None, e2, e3 140 | ) 141 | 142 | # PE_printLn'_e0c531f5 143 | def _idris_PE_95_printLn_39__95_e0c531f5(e0, e1): 144 | while True: 145 | return (65705, None, None, None, (65712, e1), (65713,)) # {U_io_bind1}, {U_{PE_printLn'_e0c531f50}1}, {U_{PE_printLn'_e0c531f51}1} 146 | 147 | # PE_show_408b7599 148 | def _idris_PE_95_show_95_408b7599(e0): 149 | while True: 150 | if e0 is not None: # Prelude.Maybe.Just 151 | in3 = e0 152 | aux1 = _idris_Prelude_46_Interfaces_46_Prelude_46_Show_46__64_Prelude_46_Interfaces_46_Ord_36_Prec_58__33__62__61__58_0( 153 | (0,), (6,) # Prelude.Show.Open, Prelude.Show.App 154 | ) 155 | if not aux1: # Prelude.Bool.False 156 | return (u'Just' + (u' ' + APPLY0( 157 | APPLY0(_idris_Prelude_46_Show_46_showPrec(None, (0, (65714,), (65716,))), None), # constructor of Prelude.Show.Show, {U_{PE_show_408b75990}1}, {U_{PE_show_408b75992}1} 158 | in3 159 | ))) 160 | else: # Prelude.Bool.True 161 | return (u'(' + ((u'Just' + (u' ' + APPLY0( 162 | APPLY0(_idris_Prelude_46_Show_46_showPrec(None, (0, (65717,), (65719,))), None), # constructor of Prelude.Show.Show, {U_{PE_show_408b75993}1}, {U_{PE_show_408b75995}1} 163 | in3 164 | ))) + u')')) 165 | return _idris_error("unreachable due to case in tail position") 166 | else: # Prelude.Maybe.Nothing 167 | return u'Nothing' 168 | return _idris_error("unreachable due to case in tail position") 169 | 170 | # Python.Lib.Numpy.Matrix.array 171 | def _idris_Python_46_Lib_46_Numpy_46_Matrix_46_array(e0, e1, e2, e3, e4): 172 | while True: 173 | assert e3[0] == 0 # Python.Lib.Numpy.Matrix.MkDType 174 | in7, in8, in9 = e3[1:] 175 | aux1 = in7 176 | return APPLY0( 177 | _idris_Python_46_Lib_46_Numpy_46_Matrix_46_unsafeNp(None, None, None, None), 178 | _idris_Python_46_Functions_46__36__46_( 179 | None, 180 | None, 181 | (1, (0,), (65685,)), # Python.Telescope.Bind, Python.Telescope.Pi, {U_Python.Lib.Numpy.Matrix.{array1}1} 182 | _idris_Python_46_Fields_46__47__47__46_( 183 | None, 184 | None, 185 | None, 186 | _idris_unsafePerformIO(None, None, (65703, None, u'numpy')), # {U_Python.importModule1} 187 | u'array', 188 | None 189 | ), 190 | None, 191 | ( 192 | 0, # Builtins.MkDPair 193 | _idris_Python_46_Prim_46_pyList( 194 | None, 195 | APPLY0( 196 | _idris_Prelude_46_List_46_toList(None, None, (65690,)), # {U_Python.Lib.Numpy.Matrix.{array6}1} 197 | _idris_Prelude_46_Functor_46_Data_46_Vect_46__64_Prelude_46_Functor_46_Functor_36_Vect_32_n_58__33_map_58_0( 198 | None, 199 | None, 200 | None, 201 | (65681, None, None, None, None, None, None, None), # {U_Python.Lib.Numpy.Matrix.array, c1} 202 | e4 203 | ) 204 | ) 205 | ), 206 | (0, aux1, Unit) # Builtins.MkDPair 207 | ) 208 | ) 209 | ) 210 | 211 | # assert_unreachable 212 | def _idris_assert_95_unreachable(): 213 | while True: 214 | return None 215 | 216 | # call__IO 217 | def _idris_call_95__95_IO(e0, e1, e2): 218 | while True: 219 | return APPLY0(e2, None) 220 | 221 | # Python.Lib.Numpy.Matrix.dot 222 | def _idris_Python_46_Lib_46_Numpy_46_Matrix_46_dot(e0, e1, e2, e3, e4, e5, e6): 223 | while True: 224 | return APPLY0( 225 | _idris_Python_46_Lib_46_Numpy_46_Matrix_46_unsafeNp(None, None, None, None), 226 | _idris_Python_46_Functions_46__36__46_( 227 | None, 228 | None, 229 | (1, (0,), (65692,)), # Python.Telescope.Bind, Python.Telescope.Pi, {U_Python.Lib.Numpy.Matrix.{dot1}1} 230 | _idris_Python_46_Fields_46__47__46_( 231 | None, 232 | None, 233 | _idris_unsafePerformIO(None, None, (65703, None, u'numpy')), # {U_Python.importModule1} 234 | u'dot', 235 | None 236 | ), 237 | None, 238 | (0, e5, (0, e6, Unit)) # Builtins.MkDPair, Builtins.MkDPair 239 | ) 240 | ) 241 | 242 | # Python.Lib.Numpy.Matrix.DType.dtFromDouble 243 | def _idris_Python_46_Lib_46_Numpy_46_Matrix_46_DType_46_dtFromDouble(e0, e1): 244 | while True: 245 | assert e1[0] == 0 # Python.Lib.Numpy.Matrix.MkDType 246 | in0, in1, in2 = e1[1:] 247 | return in2 248 | return _idris_error("unreachable due to case in tail position") 249 | 250 | # Python.Lib.Numpy.Matrix.DType.dtFromInteger 251 | def _idris_Python_46_Lib_46_Numpy_46_Matrix_46_DType_46_dtFromInteger(e0, e1): 252 | while True: 253 | assert e1[0] == 0 # Python.Lib.Numpy.Matrix.MkDType 254 | in0, in1, in2 = e1[1:] 255 | return in1 256 | return _idris_error("unreachable due to case in tail position") 257 | 258 | # Python.Lib.Numpy.Matrix.DType.dtName 259 | def _idris_Python_46_Lib_46_Numpy_46_Matrix_46_DType_46_dtName(e0, e1): 260 | while True: 261 | assert e1[0] == 0 # Python.Lib.Numpy.Matrix.MkDType 262 | in0, in1, in2 = e1[1:] 263 | return in0 264 | return _idris_error("unreachable due to case in tail position") 265 | 266 | # Main.f 267 | def _idris_Main_46_f(e0): 268 | while True: 269 | if e0 == 0: 270 | return ConsList().cons(False).cons(True) 271 | else: 272 | in0 = (e0 - 1) 273 | e0, = in0, 274 | continue 275 | return _idris_error("unreachable due to tail call") 276 | return _idris_error("unreachable due to case in tail position") 277 | 278 | # Python.Lib.Numpy.Matrix.fill 279 | def _idris_Python_46_Lib_46_Numpy_46_Matrix_46_fill(e0, e1, e2, e3, e4): 280 | while True: 281 | return APPLY0( 282 | _idris_Python_46_Lib_46_Numpy_46_Matrix_46_unsafeNp(None, None, None, None), 283 | _idris_Python_46_Functions_46__36__46_( 284 | None, 285 | None, 286 | (1, (0,), (65694,)), # Python.Telescope.Bind, Python.Telescope.Pi, {U_Python.Lib.Numpy.Matrix.{fill1}1} 287 | _idris_Python_46_Fields_46__47__46_( 288 | None, 289 | None, 290 | _idris_unsafePerformIO(None, None, (65703, None, u'numpy')), # {U_Python.importModule1} 291 | u'tile', 292 | None 293 | ), 294 | None, 295 | (0, e4, (0, (e1, e0), Unit)) # Builtins.MkDPair, Builtins.MkDPair 296 | ) 297 | ) 298 | 299 | # Prelude.Foldable.foldr 300 | def _idris_Prelude_46_Foldable_46_foldr(e0, e1, e2, e3): 301 | while True: 302 | return APPLY0(APPLY0(e1, e2), e3) 303 | 304 | # Data.Vect.foldrImpl 305 | def _idris_Data_46_Vect_46_foldrImpl(e0, e1, e2, e3, e4, e5, e6): 306 | while True: 307 | if e6[0] == 1: # Data.Vect.:: 308 | in0, in1 = e6[1:] 309 | e0, e1, e2, e3, e4, e5, e6, = None, None, None, e3, e4, (65671, None, None, None, e5, APPLY0(e3, in0)), in1, # {U_Prelude.Basics..1} 310 | continue 311 | return _idris_error("unreachable due to tail call") 312 | else: # Data.Vect.Nil 313 | return APPLY0(e5, e4) 314 | return _idris_error("unreachable due to case in tail position") 315 | 316 | # Python.Lib.Numpy.Matrix.fromDouble 317 | def _idris_Python_46_Lib_46_Numpy_46_Matrix_46_fromDouble(e0, e1, e2, e3): 318 | while True: 319 | assert e1[0] == 0 # Python.Lib.Numpy.Matrix.MkDType 320 | in0, in1, in2 = e1[1:] 321 | aux1 = in2 322 | return (65671, None, None, None, (65682, e2, e3, None, None), aux1) # {U_Prelude.Basics..1}, {U_Python.Lib.Numpy.Matrix.fill1} 323 | 324 | # Python.Lib.Numpy.Matrix.fromInteger 325 | def _idris_Python_46_Lib_46_Numpy_46_Matrix_46_fromInteger(e0, e1, e2, e3): 326 | while True: 327 | assert e1[0] == 0 # Python.Lib.Numpy.Matrix.MkDType 328 | in0, in1, in2 = e1[1:] 329 | aux1 = in1 330 | return (65671, None, None, None, (65682, e2, e3, None, None), aux1) # {U_Prelude.Basics..1}, {U_Python.Lib.Numpy.Matrix.fill1} 331 | 332 | # Prelude.Maybe.fromMaybe 333 | def _idris_Prelude_46_Maybe_46_fromMaybe(e0, e1, e2): 334 | while True: 335 | if e2 is not None: # Prelude.Maybe.Just 336 | in0 = e2 337 | return in0 338 | else: # Prelude.Maybe.Nothing 339 | return EVAL0(e1) 340 | return _idris_error("unreachable due to case in tail position") 341 | 342 | # Python.getGlobal 343 | def _idris_Python_46_getGlobal(e0, e1): 344 | while True: 345 | return _idris_unsafePerformIO(None, None, (65704, e1)) # {U_Python.{getGlobal0}1} 346 | 347 | # Prelude.Basics.id 348 | def _idris_Prelude_46_Basics_46_id(e0, e1): 349 | while True: 350 | return e1 351 | 352 | # Prelude.Bool.ifThenElse 353 | def _idris_Prelude_46_Bool_46_ifThenElse(e0, e1, e2, e3): 354 | while True: 355 | if not e1: # Prelude.Bool.False 356 | return EVAL0(e3) 357 | else: # Prelude.Bool.True 358 | return EVAL0(e2) 359 | return _idris_error("unreachable due to case in tail position") 360 | 361 | # Python.importModule 362 | def _idris_Python_46_importModule(e0, e1, _idris_w): 363 | while True: 364 | return _idris_pymodule(e1) 365 | 366 | # Prelude.Interfaces.intToBool 367 | def _idris_Prelude_46_Interfaces_46_intToBool(e0): 368 | while True: 369 | if e0 == 0: 370 | return False 371 | else: 372 | return True 373 | return _idris_error("unreachable due to case in tail position") 374 | 375 | # io_bind 376 | def _idris_io_95_bind(e0, e1, e2, e3, e4, _idris_w): 377 | while True: 378 | return APPLY0(io_bind2(e0, e1, e2, e3, e4, _idris_w), APPLY0(e3, _idris_w)) 379 | 380 | # io_pure 381 | def _idris_io_95_pure(e0, e1, e2, _idris_w): 382 | while True: 383 | return e2 384 | 385 | # Main.main 386 | def _idris_Main_46_main(): 387 | while True: 388 | return ( 389 | 65705, # {U_io_bind1} 390 | None, 391 | None, 392 | None, 393 | _idris_PE_95_printLn_39__95_e0c531f5(None, _idris_Main_46_f(4)), 394 | (65669,) # {U_Main.{main8}1} 395 | ) 396 | 397 | # mkForeignPrim 398 | def _idris_mkForeignPrim(): 399 | while True: 400 | return None 401 | 402 | # Python.Lib.Numpy.Matrix.nda 403 | def _idris_Python_46_Lib_46_Numpy_46_Matrix_46_nda(): 404 | while True: 405 | return _idris_Python_46_Fields_46__47__46_( 406 | None, 407 | None, 408 | _idris_unsafePerformIO(None, None, (65703, None, u'numpy')), # {U_Python.importModule1} 409 | u'ndarray', 410 | None 411 | ) 412 | 413 | # Python.Lib.Numpy.Matrix.op 414 | def _idris_Python_46_Lib_46_Numpy_46_Matrix_46_op(e0, e1, e2, e3, e4, e5, e6, e7): 415 | while True: 416 | return APPLY0( 417 | _idris_Python_46_Lib_46_Numpy_46_Matrix_46_unsafeNp(None, None, None, None), 418 | _idris_Python_46_Functions_46__36__46_( 419 | None, 420 | None, 421 | (1, (0,), (65696,)), # Python.Telescope.Bind, Python.Telescope.Pi, {U_Python.Lib.Numpy.Matrix.{op1}1} 422 | _idris_Python_46_Fields_46__47__46_( 423 | None, 424 | None, 425 | _idris_Python_46_Lib_46_Numpy_46_Matrix_46_nda(), 426 | e4, 427 | None 428 | ), 429 | None, 430 | (0, e6, (0, e7, Unit)) # Builtins.MkDPair, Builtins.MkDPair 431 | ) 432 | ) 433 | 434 | # Prelude.Show.precCon 435 | def _idris_Prelude_46_Show_46_precCon(e0): 436 | while True: 437 | if e0[0] == 6: # Prelude.Show.App 438 | return 6 439 | elif e0[0] == 3: # Prelude.Show.Backtick 440 | return 3 441 | elif e0[0] == 2: # Prelude.Show.Dollar 442 | return 2 443 | elif e0[0] == 1: # Prelude.Show.Eq 444 | return 1 445 | elif e0[0] == 0: # Prelude.Show.Open 446 | return 0 447 | elif e0[0] == 5: # Prelude.Show.PrefixMinus 448 | return 5 449 | else: # Prelude.Show.User 450 | in0 = e0[1] 451 | return 4 452 | return _idris_error("unreachable due to case in tail position") 453 | 454 | # prim__asPtr 455 | def _idris_prim_95__95_asPtr(op0): 456 | while True: 457 | return _idris_error("unimplemented external: prim__asPtr") 458 | 459 | # prim__concat 460 | def _idris_prim_95__95_concat(op0, op1): 461 | while True: 462 | return (op0 + op1) 463 | 464 | # prim__eqBigInt 465 | def _idris_prim_95__95_eqBigInt(op0, op1): 466 | while True: 467 | return (op0 == op1) 468 | 469 | # prim__eqManagedPtr 470 | def _idris_prim_95__95_eqManagedPtr(op0, op1): 471 | while True: 472 | return _idris_error("unimplemented external: prim__eqManagedPtr") 473 | 474 | # prim__eqPtr 475 | def _idris_prim_95__95_eqPtr(op0, op1): 476 | while True: 477 | return _idris_error("unimplemented external: prim__eqPtr") 478 | 479 | # prim__null 480 | def _idris_prim_95__95_null(): 481 | while True: 482 | return None 483 | 484 | # prim__peek16 485 | def _idris_prim_95__95_peek16(op0, op1, op2): 486 | while True: 487 | return _idris_error("unimplemented external: prim__peek16") 488 | 489 | # prim__peek32 490 | def _idris_prim_95__95_peek32(op0, op1, op2): 491 | while True: 492 | return _idris_error("unimplemented external: prim__peek32") 493 | 494 | # prim__peek64 495 | def _idris_prim_95__95_peek64(op0, op1, op2): 496 | while True: 497 | return _idris_error("unimplemented external: prim__peek64") 498 | 499 | # prim__peek8 500 | def _idris_prim_95__95_peek8(op0, op1, op2): 501 | while True: 502 | return _idris_error("unimplemented external: prim__peek8") 503 | 504 | # prim__peekDouble 505 | def _idris_prim_95__95_peekDouble(op0, op1, op2): 506 | while True: 507 | return _idris_error("unimplemented external: prim__peekDouble") 508 | 509 | # prim__peekPtr 510 | def _idris_prim_95__95_peekPtr(op0, op1, op2): 511 | while True: 512 | return _idris_error("unimplemented external: prim__peekPtr") 513 | 514 | # prim__peekSingle 515 | def _idris_prim_95__95_peekSingle(op0, op1, op2): 516 | while True: 517 | return _idris_error("unimplemented external: prim__peekSingle") 518 | 519 | # prim__poke16 520 | def _idris_prim_95__95_poke16(op0, op1, op2, op3): 521 | while True: 522 | return _idris_error("unimplemented external: prim__poke16") 523 | 524 | # prim__poke32 525 | def _idris_prim_95__95_poke32(op0, op1, op2, op3): 526 | while True: 527 | return _idris_error("unimplemented external: prim__poke32") 528 | 529 | # prim__poke64 530 | def _idris_prim_95__95_poke64(op0, op1, op2, op3): 531 | while True: 532 | return _idris_error("unimplemented external: prim__poke64") 533 | 534 | # prim__poke8 535 | def _idris_prim_95__95_poke8(op0, op1, op2, op3): 536 | while True: 537 | return _idris_error("unimplemented external: prim__poke8") 538 | 539 | # prim__pokeDouble 540 | def _idris_prim_95__95_pokeDouble(op0, op1, op2, op3): 541 | while True: 542 | return _idris_error("unimplemented external: prim__pokeDouble") 543 | 544 | # prim__pokePtr 545 | def _idris_prim_95__95_pokePtr(op0, op1, op2, op3): 546 | while True: 547 | return _idris_error("unimplemented external: prim__pokePtr") 548 | 549 | # prim__pokeSingle 550 | def _idris_prim_95__95_pokeSingle(op0, op1, op2, op3): 551 | while True: 552 | return _idris_error("unimplemented external: prim__pokeSingle") 553 | 554 | # prim__ptrOffset 555 | def _idris_prim_95__95_ptrOffset(op0, op1): 556 | while True: 557 | return _idris_error("unimplemented external: prim__ptrOffset") 558 | 559 | # prim__readFile 560 | def _idris_prim_95__95_readFile(op0, op1): 561 | while True: 562 | return _idris_error("unimplemented external: prim__readFile") 563 | 564 | # prim__registerPtr 565 | def _idris_prim_95__95_registerPtr(op0, op1): 566 | while True: 567 | return _idris_error("unimplemented external: prim__registerPtr") 568 | 569 | # prim__sizeofPtr 570 | def _idris_prim_95__95_sizeofPtr(): 571 | while True: 572 | return _idris_error("unimplemented external: prim__sizeofPtr") 573 | 574 | # prim__sltBigInt 575 | def _idris_prim_95__95_sltBigInt(op0, op1): 576 | while True: 577 | return (op0 < op1) 578 | 579 | # prim__stderr 580 | def _idris_prim_95__95_stderr(): 581 | while True: 582 | return _idris_error("unimplemented external: prim__stderr") 583 | 584 | # prim__stdin 585 | def _idris_prim_95__95_stdin(): 586 | while True: 587 | return _idris_error("unimplemented external: prim__stdin") 588 | 589 | # prim__stdout 590 | def _idris_prim_95__95_stdout(): 591 | while True: 592 | return _idris_error("unimplemented external: prim__stdout") 593 | 594 | # prim__toFloatBigInt 595 | def _idris_prim_95__95_toFloatBigInt(op0): 596 | while True: 597 | return float(op0) 598 | 599 | # prim__vm 600 | def _idris_prim_95__95_vm(op0): 601 | while True: 602 | return _idris_error("unimplemented external: prim__vm") 603 | 604 | # prim__writeFile 605 | def _idris_prim_95__95_writeFile(op0, op1, op2): 606 | while True: 607 | return _idris_error("unimplemented external: prim__writeFile") 608 | 609 | # prim__writeString 610 | def _idris_prim_95__95_writeString(op0, op1): 611 | while True: 612 | return sys.stdout.write(op1) 613 | 614 | # prim_io_bind 615 | def _idris_prim_95_io_95_bind(e0, e1, e2, e3): 616 | while True: 617 | return APPLY0(e3, e2) 618 | 619 | # prim_write 620 | def _idris_prim_95_write(e0, e1, _idris_w): 621 | while True: 622 | return sys.stdout.write(e1) 623 | 624 | # Prelude.Interactive.putStr' 625 | def _idris_Prelude_46_Interactive_46_putStr_39_(e0, e1): 626 | while True: 627 | return (65705, None, None, None, (65707, None, e1), (65674,)) # {U_io_bind1}, {U_prim_write1}, {U_Prelude.Interactive.{putStr'0}1} 628 | 629 | # Python.Prim.pyList 630 | def _idris_Python_46_Prim_46_pyList(e0, e1): 631 | while True: 632 | return _idris_unsafePerformIO( 633 | None, 634 | None, 635 | _idris_Python_46_Functions_46__36__46_( 636 | None, 637 | None, 638 | (1, (1,), (65702,)), # Python.Telescope.Bind, Python.Telescope.Forall, {U_Python.Prim.{pyList1}1} 639 | _idris_Python_46_Fields_46__47__46_( 640 | None, 641 | None, 642 | _idris_Python_46_getGlobal(None, u'__builtins__'), 643 | u'list', 644 | None 645 | ), 646 | None, 647 | (0, (0,), (0, e1, Unit)) # Builtins.MkDPair, Data.Erased.Erase, Builtins.MkDPair 648 | ) 649 | ) 650 | 651 | # Python.Lib.Numpy.Matrix.reshape 652 | def _idris_Python_46_Lib_46_Numpy_46_Matrix_46_reshape( 653 | e0, e1, e2, e3, e4, e5, e6, e7 654 | ): 655 | while True: 656 | return APPLY0( 657 | _idris_Python_46_Lib_46_Numpy_46_Matrix_46_unsafeNp(None, None, None, None), 658 | _idris_Python_46_Functions_46__36__46_( 659 | None, 660 | None, 661 | (1, (0,), (65698,)), # Python.Telescope.Bind, Python.Telescope.Pi, {U_Python.Lib.Numpy.Matrix.{reshape1}1} 662 | _idris_Python_46_Fields_46__47__46_( 663 | None, 664 | None, 665 | _idris_unsafePerformIO(None, None, (65703, None, u'numpy')), # {U_Python.importModule1} 666 | u'reshape', 667 | None 668 | ), 669 | None, 670 | (0, e6, (0, (e3, e2), Unit)) # Builtins.MkDPair, Builtins.MkDPair 671 | ) 672 | ) 673 | 674 | # run__IO 675 | def _idris_run_95__95_IO(e0, e1): 676 | while True: 677 | return APPLY0(e1, None) 678 | 679 | # Prelude.Show.show 680 | def _idris_Prelude_46_Show_46_show(e0, e1): 681 | while True: 682 | assert e1[0] == 0 # constructor of Prelude.Show.Show 683 | in0, in1 = e1[1:] 684 | return in0 685 | return _idris_error("unreachable due to case in tail position") 686 | 687 | # Prelude.Show.showParens 688 | def _idris_Prelude_46_Show_46_showParens(e0, e1): 689 | while True: 690 | if not e0: # Prelude.Bool.False 691 | return e1 692 | else: # Prelude.Bool.True 693 | return (u'(' + (e1 + u')')) 694 | return _idris_error("unreachable due to case in tail position") 695 | 696 | # Prelude.Show.showPrec 697 | def _idris_Prelude_46_Show_46_showPrec(e0, e1): 698 | while True: 699 | assert e1[0] == 0 # constructor of Prelude.Show.Show 700 | in0, in1 = e1[1:] 701 | return in1 702 | return _idris_error("unreachable due to case in tail position") 703 | 704 | # Python.Functions.strip 705 | def _idris_Python_46_Functions_46_strip(e0, e1, e2): 706 | while True: 707 | if e1[0] == 1: # Python.Telescope.Bind 708 | in0, in1 = e1[1:] 709 | if in0[0] == 2: # Python.Telescope.Default 710 | in2 = in0[1] 711 | assert e2[0] == 0 # Builtins.MkDPair 712 | in3, in4 = e2[1:] 713 | if in3 is not None: # Prelude.Maybe.Just 714 | in5 = in3 715 | aux1 = in5 716 | else: # Prelude.Maybe.Nothing 717 | aux1 = _idris_Python_46_Functions_46__123_strip0_125_(in2) 718 | return _idris_Python_46_Functions_46_strip(None, APPLY0(in1, aux1), in4).cons(in3) 719 | return _idris_error("unreachable due to case in tail position") 720 | elif in0[0] == 1: # Python.Telescope.Forall 721 | assert e2[0] == 0 # Builtins.MkDPair 722 | in6, in7 = e2[1:] 723 | e0, e1, e2, = None, APPLY0(in1, in6), in7, 724 | continue 725 | return _idris_error("unreachable due to tail call") 726 | return _idris_error("unreachable due to case in tail position") 727 | else: # Python.Telescope.Pi 728 | assert e2[0] == 0 # Builtins.MkDPair 729 | in8, in9 = e2[1:] 730 | return _idris_Python_46_Functions_46_strip(None, APPLY0(in1, in8), in9).cons(in8) 731 | return _idris_error("unreachable due to case in tail position") 732 | return _idris_error("unreachable due to case in tail position") 733 | else: # Python.Telescope.Return 734 | return ConsList() 735 | return _idris_error("unreachable due to case in tail position") 736 | 737 | # Prelude.List.toList 738 | def _idris_Prelude_46_List_46_toList(e0, e1, e2): 739 | while True: 740 | return APPLY0( 741 | APPLY0(_idris_Prelude_46_Foldable_46_foldr(None, e2, None, None), (65676,)), # {U_Prelude.List.{toList1}1} 742 | ConsList() 743 | ) 744 | 745 | # Python.Lib.Numpy.Matrix.transpose 746 | def _idris_Python_46_Lib_46_Numpy_46_Matrix_46_transpose(e0, e1, e2, e3, e4): 747 | while True: 748 | return APPLY0( 749 | _idris_Python_46_Lib_46_Numpy_46_Matrix_46_unsafeNp(None, None, None, None), 750 | _idris_Python_46_Functions_46__36__46_( 751 | None, 752 | None, 753 | (1, (0,), (65699,)), # Python.Telescope.Bind, Python.Telescope.Pi, {U_Python.Lib.Numpy.Matrix.{transpose0}1} 754 | _idris_Python_46_Fields_46__47__46_( 755 | None, 756 | None, 757 | _idris_unsafePerformIO(None, None, (65703, None, u'numpy')), # {U_Python.importModule1} 758 | u'transpose', 759 | None 760 | ), 761 | None, 762 | (0, e4, Unit) # Builtins.MkDPair 763 | ) 764 | ) 765 | 766 | # Python.IO.unRaw 767 | def _idris_Python_46_IO_46_unRaw(e0, e1): 768 | while True: 769 | return e1 770 | 771 | # Python.Lib.Numpy.Matrix.unsafeNp 772 | def _idris_Python_46_Lib_46_Numpy_46_Matrix_46_unsafeNp(e0, e1, e2, e3): 773 | while True: 774 | return (65671, None, None, None, (65700,), (65708, None, None)) # {U_Prelude.Basics..1}, {U_Python.Lib.Numpy.Matrix.{unsafeNp0}1}, {U_unsafePerformIO1} 775 | 776 | # unsafePerformIO 777 | def _idris_unsafePerformIO(e0, e1, e2): 778 | while True: 779 | return APPLY0(unsafePerformIO1(e0, e1, e2), APPLY0(e2, None)) 780 | 781 | # unsafePerformPrimIO 782 | def _idris_unsafePerformPrimIO(): 783 | while True: 784 | return None 785 | 786 | # world 787 | def _idris_world(e0): 788 | while True: 789 | return e0 790 | 791 | # Main.xs 792 | def _idris_Main_46_xs(): 793 | while True: 794 | return _idris_Python_46_Lib_46_Numpy_46_Matrix_46_array( 795 | None, 796 | None, 797 | None, 798 | (0, u'float', (65670,), (65672, None)), # Python.Lib.Numpy.Matrix.MkDType, {U_Main.{xs0}1}, {U_Prelude.Basics.id1} 799 | ( 800 | 1, # Data.Vect.:: 801 | (1, 1.0, (1, -2.1, (1, 3.3, (1, -0.1, (0,))))), # Data.Vect.::, Data.Vect.::, Data.Vect.::, Data.Vect.::, Data.Vect.Nil 802 | ( 803 | 1, # Data.Vect.:: 804 | (1, 3.5, (1, 7.0, (1, 0.0, (1, -5.2, (0,))))), # Data.Vect.::, Data.Vect.::, Data.Vect.::, Data.Vect.::, Data.Vect.Nil 805 | (1, (1, 0.5, (1, 7.2, (1, -1.1, (1, 0.0, (0,))))), (0,)) # Data.Vect.::, Data.Vect.::, Data.Vect.::, Data.Vect.::, Data.Vect.::, Data.Vect.Nil, Data.Vect.Nil 806 | ) 807 | ) 808 | ) 809 | 810 | # Main.ys 811 | def _idris_Main_46_ys(): 812 | while True: 813 | return _idris_Python_46_Lib_46_Numpy_46_Matrix_46_reshape( 814 | None, 815 | None, 816 | 2, 817 | 6, 818 | None, 819 | None, 820 | _idris_Main_46_xs(), 821 | None 822 | ) 823 | 824 | # Main.zs 825 | def _idris_Main_46_zs(): 826 | while True: 827 | return _idris_Python_46_Lib_46_Numpy_46_Matrix_46_reshape( 828 | None, 829 | None, 830 | 3, 831 | 4, 832 | None, 833 | None, 834 | _idris_Main_46_ys(), 835 | None 836 | ) 837 | 838 | # Prelude.Bool.|| 839 | def _idris_Prelude_46_Bool_46__124__124_(e0, e1): 840 | while True: 841 | if not e0: # Prelude.Bool.False 842 | return EVAL0(e1) 843 | else: # Prelude.Bool.True 844 | return True 845 | return _idris_error("unreachable due to case in tail position") 846 | 847 | # Python.Functions.{$.0} 848 | def _idris_Python_46_Functions_46__123__36__46_0_125_(e3, e2, e5, in0): 849 | while True: 850 | return _idris_call(e3, _idris_Python_46_Functions_46_strip(None, e2, e5)) 851 | 852 | # Python.Fields.{/.0} 853 | def _idris_Python_46_Fields_46__123__47__46_0_125_(e2, e3, in0): 854 | while True: 855 | return getattr(e2, e3) 856 | 857 | # Python.Fields.{//.0} 858 | def _idris_Python_46_Fields_46__123__47__47__46_0_125_(e3, e4, in0): 859 | while True: 860 | return getattr(e3, e4) 861 | 862 | # {APPLY0} 863 | def APPLY0(fn0, arg0): 864 | while True: 865 | if fn0[0] < 65695: 866 | if fn0[0] < 65678: 867 | if fn0[0] < 65669: 868 | if fn0[0] < 65665: 869 | if fn0[0] < 65663: 870 | if fn0[0] == 65661: # {U_Main.{main0}1} 871 | return _idris_Main_46__123_main0_125_(arg0) 872 | else: # {U_Main.{main1}1} 873 | return _idris_Main_46__123_main1_125_(arg0) 874 | else: 875 | if fn0[0] == 65663: # {U_Main.{main2}1} 876 | return _idris_Main_46__123_main2_125_(arg0) 877 | else: # {U_Main.{main3}1} 878 | return _idris_Main_46__123_main3_125_(arg0) 879 | else: 880 | if fn0[0] < 65667: 881 | if fn0[0] == 65665: # {U_Main.{main4}1} 882 | return _idris_Main_46__123_main4_125_(arg0) 883 | else: # {U_Main.{main5}1} 884 | return _idris_Main_46__123_main5_125_(arg0) 885 | else: 886 | if fn0[0] == 65667: # {U_Main.{main6}1} 887 | return _idris_Main_46__123_main6_125_(arg0) 888 | else: # {U_Main.{main7}1} 889 | return _idris_Main_46__123_main7_125_(arg0) 890 | else: 891 | if fn0[0] < 65673: 892 | if fn0[0] < 65671: 893 | if fn0[0] == 65669: # {U_Main.{main8}1} 894 | return _idris_Main_46__123_main8_125_(arg0) 895 | else: # {U_Main.{xs0}1} 896 | return _idris_Main_46__123_xs0_125_(arg0) 897 | else: 898 | if fn0[0] == 65671: # {U_Prelude.Basics..1} 899 | P_c0, P_c1, P_c2, P_c3, P_c4 = fn0[1:] 900 | return _idris_Prelude_46_Basics_46__46_(P_c0, P_c1, P_c2, P_c3, P_c4, arg0) 901 | else: # {U_Prelude.Basics.id1} 902 | P_c0 = fn0[1] 903 | return _idris_Prelude_46_Basics_46_id(P_c0, arg0) 904 | else: 905 | if fn0[0] < 65675: 906 | if fn0[0] == 65673: # {U_Prelude.Functor.{Prelude.Monad.@Prelude.Functor.Functor$IO' ffi:!map:0_lam0}1} 907 | P_c0 = fn0[1] 908 | return _idris_Prelude_46_Functor_46__123_Prelude_46_Monad_46__64_Prelude_46_Functor_46_Functor_36_IO_39__32_ffi_58__33_map_58_0_95_lam0_125_( 909 | P_c0, arg0 910 | ) 911 | else: # {U_Prelude.Interactive.{putStr'0}1} 912 | return _idris_Prelude_46_Interactive_46__123_putStr_39_0_125_(arg0) 913 | else: 914 | if fn0[0] == 65675: # {U_Prelude.List.{toList0}1} 915 | P_c0 = fn0[1] 916 | return _idris_Prelude_46_List_46__123_toList0_125_(P_c0, arg0) 917 | elif fn0[0] == 65676: # {U_Prelude.List.{toList1}1} 918 | return _idris_Prelude_46_List_46__123_toList1_125_(arg0) 919 | else: # {U_Python.Fields.{/.0}1} 920 | P_c0, P_c1 = fn0[1:] 921 | return _idris_Python_46_Fields_46__123__47__46_0_125_(P_c0, P_c1, arg0) 922 | else: 923 | if fn0[0] < 65686: 924 | if fn0[0] < 65682: 925 | if fn0[0] < 65680: 926 | if fn0[0] == 65678: # {U_Python.Fields.{//.0}1} 927 | P_c0, P_c1 = fn0[1:] 928 | return _idris_Python_46_Fields_46__123__47__47__46_0_125_(P_c0, P_c1, arg0) 929 | else: # {U_Python.Functions.{$.0}1} 930 | P_c0, P_c1, P_c2 = fn0[1:] 931 | return _idris_Python_46_Functions_46__123__36__46_0_125_(P_c0, P_c1, P_c2, arg0) 932 | else: 933 | if fn0[0] == 65680: # {U_Python.IO.unRaw1} 934 | P_c0 = fn0[1] 935 | return _idris_Python_46_IO_46_unRaw(P_c0, arg0) 936 | else: # {U_Python.Lib.Numpy.Matrix.array, c1} 937 | P_c0, P_c1, P_c2, P_c3, P_c4, P_c5, P_c6 = fn0[1:] 938 | return _idris_Python_46_Lib_46_Numpy_46_Matrix_46_array_58_c_58_0( 939 | P_c0, P_c1, P_c2, P_c3, P_c4, P_c5, P_c6, arg0 940 | ) 941 | else: 942 | if fn0[0] < 65684: 943 | if fn0[0] == 65682: # {U_Python.Lib.Numpy.Matrix.fill1} 944 | P_c0, P_c1, P_c2, P_c3 = fn0[1:] 945 | return _idris_Python_46_Lib_46_Numpy_46_Matrix_46_fill(P_c0, P_c1, P_c2, P_c3, arg0) 946 | else: # {U_Python.Lib.Numpy.Matrix.op1} 947 | P_c0, P_c1, P_c2, P_c3, P_c4, P_c5, P_c6 = fn0[1:] 948 | return _idris_Python_46_Lib_46_Numpy_46_Matrix_46_op( 949 | P_c0, P_c1, P_c2, P_c3, P_c4, P_c5, P_c6, arg0 950 | ) 951 | else: 952 | if fn0[0] == 65684: # {U_Python.Lib.Numpy.Matrix.{array0}1} 953 | return _idris_Python_46_Lib_46_Numpy_46_Matrix_46__123_array0_125_(arg0) 954 | else: # {U_Python.Lib.Numpy.Matrix.{array1}1} 955 | return _idris_Python_46_Lib_46_Numpy_46_Matrix_46__123_array1_125_(arg0) 956 | else: 957 | if fn0[0] < 65690: 958 | if fn0[0] < 65688: 959 | if fn0[0] == 65686: # {U_Python.Lib.Numpy.Matrix.{array2}1} 960 | P_c0, P_c1 = fn0[1:] 961 | return _idris_Python_46_Lib_46_Numpy_46_Matrix_46__123_array2_125_(P_c0, P_c1, arg0) 962 | else: # {U_Python.Lib.Numpy.Matrix.{array3}1} 963 | P_c0 = fn0[1] 964 | return _idris_Python_46_Lib_46_Numpy_46_Matrix_46__123_array3_125_(P_c0, arg0) 965 | else: 966 | if fn0[0] == 65688: # {U_Python.Lib.Numpy.Matrix.{array4}1} 967 | return _idris_Python_46_Lib_46_Numpy_46_Matrix_46__123_array4_125_(arg0) 968 | else: # {U_Python.Lib.Numpy.Matrix.{array5}1} 969 | return _idris_Python_46_Lib_46_Numpy_46_Matrix_46__123_array5_125_(arg0) 970 | else: 971 | if fn0[0] < 65692: 972 | if fn0[0] == 65690: # {U_Python.Lib.Numpy.Matrix.{array6}1} 973 | return _idris_Python_46_Lib_46_Numpy_46_Matrix_46__123_array6_125_(arg0) 974 | else: # {U_Python.Lib.Numpy.Matrix.{dot0}1} 975 | return _idris_Python_46_Lib_46_Numpy_46_Matrix_46__123_dot0_125_(arg0) 976 | else: 977 | if fn0[0] == 65692: # {U_Python.Lib.Numpy.Matrix.{dot1}1} 978 | return _idris_Python_46_Lib_46_Numpy_46_Matrix_46__123_dot1_125_(arg0) 979 | elif fn0[0] == 65693: # {U_Python.Lib.Numpy.Matrix.{fill0}1} 980 | return _idris_Python_46_Lib_46_Numpy_46_Matrix_46__123_fill0_125_(arg0) 981 | else: # {U_Python.Lib.Numpy.Matrix.{fill1}1} 982 | return _idris_Python_46_Lib_46_Numpy_46_Matrix_46__123_fill1_125_(arg0) 983 | else: 984 | if fn0[0] < 65712: 985 | if fn0[0] < 65703: 986 | if fn0[0] < 65699: 987 | if fn0[0] < 65697: 988 | if fn0[0] == 65695: # {U_Python.Lib.Numpy.Matrix.{op0}1} 989 | return _idris_Python_46_Lib_46_Numpy_46_Matrix_46__123_op0_125_(arg0) 990 | else: # {U_Python.Lib.Numpy.Matrix.{op1}1} 991 | return _idris_Python_46_Lib_46_Numpy_46_Matrix_46__123_op1_125_(arg0) 992 | else: 993 | if fn0[0] == 65697: # {U_Python.Lib.Numpy.Matrix.{reshape0}1} 994 | return _idris_Python_46_Lib_46_Numpy_46_Matrix_46__123_reshape0_125_(arg0) 995 | else: # {U_Python.Lib.Numpy.Matrix.{reshape1}1} 996 | return _idris_Python_46_Lib_46_Numpy_46_Matrix_46__123_reshape1_125_(arg0) 997 | else: 998 | if fn0[0] < 65701: 999 | if fn0[0] == 65699: # {U_Python.Lib.Numpy.Matrix.{transpose0}1} 1000 | return _idris_Python_46_Lib_46_Numpy_46_Matrix_46__123_transpose0_125_(arg0) 1001 | else: # {U_Python.Lib.Numpy.Matrix.{unsafeNp0}1} 1002 | return _idris_Python_46_Lib_46_Numpy_46_Matrix_46__123_unsafeNp0_125_(arg0) 1003 | else: 1004 | if fn0[0] == 65701: # {U_Python.Prim.{pyList0}1} 1005 | return _idris_Python_46_Prim_46__123_pyList0_125_(arg0) 1006 | else: # {U_Python.Prim.{pyList1}1} 1007 | return _idris_Python_46_Prim_46__123_pyList1_125_(arg0) 1008 | else: 1009 | if fn0[0] < 65707: 1010 | if fn0[0] < 65705: 1011 | if fn0[0] == 65703: # {U_Python.importModule1} 1012 | P_c0, P_c1 = fn0[1:] 1013 | return _idris_Python_46_importModule(P_c0, P_c1, arg0) 1014 | else: # {U_Python.{getGlobal0}1} 1015 | P_c0 = fn0[1] 1016 | return _idris_Python_46__123_getGlobal0_125_(P_c0, arg0) 1017 | else: 1018 | if fn0[0] == 65705: # {U_io_bind1} 1019 | P_c0, P_c1, P_c2, P_c3, P_c4 = fn0[1:] 1020 | return _idris_io_95_bind(P_c0, P_c1, P_c2, P_c3, P_c4, arg0) 1021 | else: # {U_io_pure1} 1022 | P_c0, P_c1, P_c2 = fn0[1:] 1023 | return _idris_io_95_pure(P_c0, P_c1, P_c2, arg0) 1024 | else: 1025 | if fn0[0] < 65709: 1026 | if fn0[0] == 65707: # {U_prim_write1} 1027 | P_c0, P_c1 = fn0[1:] 1028 | return _idris_prim_95_write(P_c0, P_c1, arg0) 1029 | else: # {U_unsafePerformIO1} 1030 | P_c0, P_c1 = fn0[1:] 1031 | return _idris_unsafePerformIO(P_c0, P_c1, arg0) 1032 | else: 1033 | if fn0[0] == 65709: # {U_{PE_Prelude.Show.List a implementation of Prelude.Show.Show, method show_d2fe22140}1} 1034 | return _idris__123_PE_95_Prelude_46_Show_46_List_32_a_32_implementation_32_of_32_Prelude_46_Show_46_Show_44__32_method_32_show_95_d2fe22140_125_( 1035 | arg0 1036 | ) 1037 | elif fn0[0] == 65710: # {U_{PE_Prelude.Show.List a implementation of Prelude.Show.Show, method show_d2fe22141}1} 1038 | return _idris__123_PE_95_Prelude_46_Show_46_List_32_a_32_implementation_32_of_32_Prelude_46_Show_46_Show_44__32_method_32_show_95_d2fe22141_125_( 1039 | arg0 1040 | ) 1041 | else: # {U_{PE_Prelude.Show.List a implementation of Prelude.Show.Show, method show_d2fe22142}1} 1042 | return _idris__123_PE_95_Prelude_46_Show_46_List_32_a_32_implementation_32_of_32_Prelude_46_Show_46_Show_44__32_method_32_show_95_d2fe22142_125_( 1043 | arg0 1044 | ) 1045 | else: 1046 | if fn0[0] < 65720: 1047 | if fn0[0] < 65716: 1048 | if fn0[0] < 65714: 1049 | if fn0[0] == 65712: # {U_{PE_printLn'_e0c531f50}1} 1050 | P_c0 = fn0[1] 1051 | return _idris__123_PE_95_printLn_39__95_e0c531f50_125_(P_c0, arg0) 1052 | else: # {U_{PE_printLn'_e0c531f51}1} 1053 | return _idris__123_PE_95_printLn_39__95_e0c531f51_125_(arg0) 1054 | else: 1055 | if fn0[0] == 65714: # {U_{PE_show_408b75990}1} 1056 | return _idris__123_PE_95_show_95_408b75990_125_(arg0) 1057 | else: # {U_{PE_show_408b75991}1} 1058 | return _idris__123_PE_95_show_95_408b75991_125_(arg0) 1059 | else: 1060 | if fn0[0] < 65718: 1061 | if fn0[0] == 65716: # {U_{PE_show_408b75992}1} 1062 | return _idris__123_PE_95_show_95_408b75992_125_(arg0) 1063 | else: # {U_{PE_show_408b75993}1} 1064 | return _idris__123_PE_95_show_95_408b75993_125_(arg0) 1065 | else: 1066 | if fn0[0] == 65718: # {U_{PE_show_408b75994}1} 1067 | return _idris__123_PE_95_show_95_408b75994_125_(arg0) 1068 | else: # {U_{PE_show_408b75995}1} 1069 | return _idris__123_PE_95_show_95_408b75995_125_(arg0) 1070 | else: 1071 | if fn0[0] < 65724: 1072 | if fn0[0] < 65722: 1073 | if fn0[0] == 65720: # {U_{Python.Lib.Numpy.Matrix.array:c:0_lam0}1} 1074 | P_c0, P_c1 = fn0[1:] 1075 | return _idris__123_Python_46_Lib_46_Numpy_46_Matrix_46_array_58_c_58_0_95_lam0_125_( 1076 | P_c0, P_c1, arg0 1077 | ) 1078 | else: # {U_{Python.Lib.Numpy.Matrix.array:c:0_lam1}1} 1079 | P_c0 = fn0[1] 1080 | return _idris__123_Python_46_Lib_46_Numpy_46_Matrix_46_array_58_c_58_0_95_lam1_125_( 1081 | P_c0, arg0 1082 | ) 1083 | else: 1084 | if fn0[0] == 65722: # {U_{Python.Lib.Numpy.Matrix.array:c:0_lam2}1} 1085 | return _idris__123_Python_46_Lib_46_Numpy_46_Matrix_46_array_58_c_58_0_95_lam2_125_( 1086 | arg0 1087 | ) 1088 | else: # {U_{Python.Lib.Numpy.Matrix.array:c:0_lam3}1} 1089 | return _idris__123_Python_46_Lib_46_Numpy_46_Matrix_46_array_58_c_58_0_95_lam3_125_( 1090 | arg0 1091 | ) 1092 | else: 1093 | if fn0[0] < 65726: 1094 | if fn0[0] == 65724: # {U_{Python.Lib.Numpy.Matrix.array:c:0_lam4}1} 1095 | return _idris__123_Python_46_Lib_46_Numpy_46_Matrix_46_array_58_c_58_0_95_lam4_125_( 1096 | arg0 1097 | ) 1098 | else: # {U_{io_bind1}1} 1099 | P_c0, P_c1, P_c2, P_c3, P_c4, P_c5 = fn0[1:] 1100 | return io_bind1(P_c0, P_c1, P_c2, P_c3, P_c4, P_c5, arg0) 1101 | else: 1102 | if fn0[0] == 65726: # {U_{unsafePerformIO0}1} 1103 | return unsafePerformIO0(arg0) 1104 | else: # {U_Python.Lib.Numpy.Matrix.op2} 1105 | P_c0, P_c1, P_c2, P_c3, P_c4, P_c5 = fn0[1:] 1106 | return (65683, P_c0, P_c1, P_c2, P_c3, P_c4, P_c5, arg0) # {U_Python.Lib.Numpy.Matrix.op1} 1107 | return _idris_error("unreachable due to case in tail position") 1108 | 1109 | # {APPLY20} 1110 | def _idris__123_APPLY20_125_(fn0, _idris__123_arg00_125_, _idris__123_arg10_125_): 1111 | while True: 1112 | if fn0[0] == 65727: # {U_Python.Lib.Numpy.Matrix.op2} 1113 | P_c0, P_c1, P_c2, P_c3, P_c4, P_c5 = fn0[1:] 1114 | return _idris_Python_46_Lib_46_Numpy_46_Matrix_46_op( 1115 | P_c0, 1116 | P_c1, 1117 | P_c2, 1118 | P_c3, 1119 | P_c4, 1120 | P_c5, 1121 | _idris__123_arg00_125_, 1122 | _idris__123_arg10_125_ 1123 | ) 1124 | else: 1125 | return APPLY0(APPLY0(fn0, _idris__123_arg00_125_), _idris__123_arg10_125_) 1126 | return _idris_error("unreachable due to case in tail position") 1127 | 1128 | # {EVAL0} 1129 | def EVAL0(arg0): 1130 | while True: 1131 | return arg0 1132 | 1133 | # {PE_Prelude.Show.List a implementation of Prelude.Show.Show, method show_d2fe22140} 1134 | def _idris__123_PE_95_Prelude_46_Show_46_List_32_a_32_implementation_32_of_32_Prelude_46_Show_46_Show_44__32_method_32_show_95_d2fe22140_125_( 1135 | in0 1136 | ): 1137 | while True: 1138 | if not in0: # Prelude.Bool.False 1139 | return u'False' 1140 | else: # Prelude.Bool.True 1141 | return u'True' 1142 | return _idris_error("unreachable due to case in tail position") 1143 | 1144 | # {PE_printLn'_e0c531f50} 1145 | def _idris__123_PE_95_printLn_39__95_e0c531f50_125_(e1, in0): 1146 | while True: 1147 | return sys.stdout.write((_idris_PE_95_show_95_408b7599(e1) + u'\u000a')) 1148 | 1149 | # {PE_show_408b75990} 1150 | def _idris__123_PE_95_show_95_408b75990_125_(in0): 1151 | while True: 1152 | return _idris_PE_95_Prelude_46_Show_46_List_32_a_32_implementation_32_of_32_Prelude_46_Show_46_Show_44__32_method_32_show_95_d2fe2214( 1153 | in0 1154 | ) 1155 | 1156 | # Prelude.Functor.{Prelude.Monad.@Prelude.Functor.Functor$IO' ffi:!map:0_lam0} 1157 | def _idris_Prelude_46_Functor_46__123_Prelude_46_Monad_46__64_Prelude_46_Functor_46_Functor_36_IO_39__32_ffi_58__33_map_58_0_95_lam0_125_( 1158 | e3, in0 1159 | ): 1160 | while True: 1161 | return (65706, None, None, APPLY0(e3, in0)) # {U_io_pure1} 1162 | 1163 | # Prelude.Interfaces.{Prelude.Show.@Prelude.Interfaces.Ord$Prec:!>=:0_lam0} 1164 | def _idris_Prelude_46_Interfaces_46__123_Prelude_46_Show_46__64_Prelude_46_Interfaces_46_Ord_36_Prec_58__33__62__61__58_0_95_lam0_125_( 1165 | e0, e1 1166 | ): 1167 | while True: 1168 | return _idris_Prelude_46_Interfaces_46_Prelude_46_Show_46__64_Prelude_46_Interfaces_46_Eq_36_Prec_58__33__61__61__58_0( 1169 | e0, e1 1170 | ) 1171 | 1172 | # {Python.Lib.Numpy.Matrix.array:c:0_lam0} 1173 | def _idris__123_Python_46_Lib_46_Numpy_46_Matrix_46_array_58_c_58_0_95_lam0_125_( 1174 | in2, in3, in4 1175 | ): 1176 | while True: 1177 | return _idris_Data_46_Vect_46_foldrImpl(None, None, None, in2, in3, (65672, None), in4) # {U_Prelude.Basics.id1} 1178 | 1179 | # Python.Lib.Numpy.Matrix.{array0} 1180 | def _idris_Python_46_Lib_46_Numpy_46_Matrix_46__123_array0_125_(in1): 1181 | while True: 1182 | return (0,) # Python.Telescope.Return 1183 | 1184 | # Python.Lib.Numpy.Matrix.{dot0} 1185 | def _idris_Python_46_Lib_46_Numpy_46_Matrix_46__123_dot0_125_(in1): 1186 | while True: 1187 | return (0,) # Python.Telescope.Return 1188 | 1189 | # Python.Lib.Numpy.Matrix.{fill0} 1190 | def _idris_Python_46_Lib_46_Numpy_46_Matrix_46__123_fill0_125_(in1): 1191 | while True: 1192 | return (0,) # Python.Telescope.Return 1193 | 1194 | # Python.{getGlobal0} 1195 | def _idris_Python_46__123_getGlobal0_125_(e1, in0): 1196 | while True: 1197 | return _idris_get_global(e1) 1198 | 1199 | # {io_bind0} 1200 | def io_bind0(e0, e1, e2, e3, e4, _idris_w, in0): 1201 | while True: 1202 | return APPLY0(e4, in0) 1203 | 1204 | # Main.{main0} 1205 | def _idris_Main_46__123_main0_125_(in7): 1206 | while True: 1207 | return float(in7) 1208 | 1209 | # Python.Lib.Numpy.Matrix.{op0} 1210 | def _idris_Python_46_Lib_46_Numpy_46_Matrix_46__123_op0_125_(in1): 1211 | while True: 1212 | return (0,) # Python.Telescope.Return 1213 | 1214 | # Prelude.Interactive.{putStr'0} 1215 | def _idris_Prelude_46_Interactive_46__123_putStr_39_0_125_(in0): 1216 | while True: 1217 | return (65706, None, None, Unit) # {U_io_pure1} 1218 | 1219 | # Python.Prim.{pyList0} 1220 | def _idris_Python_46_Prim_46__123_pyList0_125_(in1): 1221 | while True: 1222 | return (0,) # Python.Telescope.Return 1223 | 1224 | # Python.Lib.Numpy.Matrix.{reshape0} 1225 | def _idris_Python_46_Lib_46_Numpy_46_Matrix_46__123_reshape0_125_(in1): 1226 | while True: 1227 | return (0,) # Python.Telescope.Return 1228 | 1229 | # {runMain0} 1230 | def runMain0(): 1231 | while True: 1232 | return EVAL0(APPLY0(_idris_Main_46_main(), None)) 1233 | 1234 | # Python.Functions.{strip0} 1235 | def _idris_Python_46_Functions_46__123_strip0_125_(in2): 1236 | while True: 1237 | return in2 1238 | 1239 | # Prelude.List.{toList0} 1240 | def _idris_Prelude_46_List_46__123_toList0_125_(in0, in1): 1241 | while True: 1242 | return in1.cons(in0) 1243 | 1244 | # Python.Lib.Numpy.Matrix.{transpose0} 1245 | def _idris_Python_46_Lib_46_Numpy_46_Matrix_46__123_transpose0_125_(in0): 1246 | while True: 1247 | return (0,) # Python.Telescope.Return 1248 | 1249 | # Python.Lib.Numpy.Matrix.{unsafeNp0} 1250 | def _idris_Python_46_Lib_46_Numpy_46_Matrix_46__123_unsafeNp0_125_(in0): 1251 | while True: 1252 | return in0 1253 | 1254 | # {unsafePerformIO0} 1255 | def unsafePerformIO0(in0): 1256 | while True: 1257 | return in0 1258 | 1259 | # Main.{xs0} 1260 | def _idris_Main_46__123_xs0_125_(in0): 1261 | while True: 1262 | return float(in0) 1263 | 1264 | # {PE_Prelude.Show.List a implementation of Prelude.Show.Show, method show_d2fe22141} 1265 | def _idris__123_PE_95_Prelude_46_Show_46_List_32_a_32_implementation_32_of_32_Prelude_46_Show_46_Show_44__32_method_32_show_95_d2fe22141_125_( 1266 | in2 1267 | ): 1268 | while True: 1269 | if not in2: # Prelude.Bool.False 1270 | return u'False' 1271 | else: # Prelude.Bool.True 1272 | return u'True' 1273 | return _idris_error("unreachable due to case in tail position") 1274 | 1275 | # {PE_printLn'_e0c531f51} 1276 | def _idris__123_PE_95_printLn_39__95_e0c531f51_125_(in1): 1277 | while True: 1278 | return (65706, None, None, Unit) # {U_io_pure1} 1279 | 1280 | # {PE_show_408b75991} 1281 | def _idris__123_PE_95_show_95_408b75991_125_(in2): 1282 | while True: 1283 | return _idris_PE_95_Prelude_46_Show_46_List_32_a_32_implementation_32_of_32_Prelude_46_Show_46_Show_44__32_method_32_showPrec_95_32c264fb( 1284 | None, in2 1285 | ) 1286 | 1287 | # {Python.Lib.Numpy.Matrix.array:c:0_lam1} 1288 | def _idris__123_Python_46_Lib_46_Numpy_46_Matrix_46_array_58_c_58_0_95_lam1_125_( 1289 | in2, in3 1290 | ): 1291 | while True: 1292 | return (65720, in2, in3) # {U_{Python.Lib.Numpy.Matrix.array:c:0_lam0}1} 1293 | 1294 | # Python.Lib.Numpy.Matrix.{array1} 1295 | def _idris_Python_46_Lib_46_Numpy_46_Matrix_46__123_array1_125_(in0): 1296 | while True: 1297 | return (1, (0,), (65684,)) # Python.Telescope.Bind, Python.Telescope.Pi, {U_Python.Lib.Numpy.Matrix.{array0}1} 1298 | 1299 | # Python.Lib.Numpy.Matrix.{dot1} 1300 | def _idris_Python_46_Lib_46_Numpy_46_Matrix_46__123_dot1_125_(in0): 1301 | while True: 1302 | return (1, (0,), (65691,)) # Python.Telescope.Bind, Python.Telescope.Pi, {U_Python.Lib.Numpy.Matrix.{dot0}1} 1303 | 1304 | # Python.Lib.Numpy.Matrix.{fill1} 1305 | def _idris_Python_46_Lib_46_Numpy_46_Matrix_46__123_fill1_125_(in0): 1306 | while True: 1307 | return (1, (0,), (65693,)) # Python.Telescope.Bind, Python.Telescope.Pi, {U_Python.Lib.Numpy.Matrix.{fill0}1} 1308 | 1309 | # {io_bind1} 1310 | def io_bind1(e0, e1, e2, e3, e4, _idris_w, in0): 1311 | while True: 1312 | return APPLY0(io_bind0(e0, e1, e2, e3, e4, _idris_w, in0), _idris_w) 1313 | 1314 | # Main.{main1} 1315 | def _idris_Main_46__123_main1_125_(in8): 1316 | while True: 1317 | return float(in8) 1318 | 1319 | # Python.Lib.Numpy.Matrix.{op1} 1320 | def _idris_Python_46_Lib_46_Numpy_46_Matrix_46__123_op1_125_(in0): 1321 | while True: 1322 | return (1, (0,), (65695,)) # Python.Telescope.Bind, Python.Telescope.Pi, {U_Python.Lib.Numpy.Matrix.{op0}1} 1323 | 1324 | # Python.Prim.{pyList1} 1325 | def _idris_Python_46_Prim_46__123_pyList1_125_(in0): 1326 | while True: 1327 | return (1, (0,), (65701,)) # Python.Telescope.Bind, Python.Telescope.Pi, {U_Python.Prim.{pyList0}1} 1328 | 1329 | # Python.Lib.Numpy.Matrix.{reshape1} 1330 | def _idris_Python_46_Lib_46_Numpy_46_Matrix_46__123_reshape1_125_(in0): 1331 | while True: 1332 | return (1, (0,), (65697,)) # Python.Telescope.Bind, Python.Telescope.Pi, {U_Python.Lib.Numpy.Matrix.{reshape0}1} 1333 | 1334 | # Prelude.List.{toList1} 1335 | def _idris_Prelude_46_List_46__123_toList1_125_(in0): 1336 | while True: 1337 | return (65675, in0) # {U_Prelude.List.{toList0}1} 1338 | 1339 | # {unsafePerformIO1} 1340 | def unsafePerformIO1(e0, e1, e2): 1341 | while True: 1342 | return (65726,) # {U_{unsafePerformIO0}1} 1343 | 1344 | # {PE_Prelude.Show.List a implementation of Prelude.Show.Show, method show_d2fe22142} 1345 | def _idris__123_PE_95_Prelude_46_Show_46_List_32_a_32_implementation_32_of_32_Prelude_46_Show_46_Show_44__32_method_32_show_95_d2fe22142_125_( 1346 | in1 1347 | ): 1348 | while True: 1349 | return (65710,) # {U_{PE_Prelude.Show.List a implementation of Prelude.Show.Show, method show_d2fe22141}1} 1350 | 1351 | # {PE_show_408b75992} 1352 | def _idris__123_PE_95_show_95_408b75992_125_(in1): 1353 | while True: 1354 | return (65715,) # {U_{PE_show_408b75991}1} 1355 | 1356 | # {Python.Lib.Numpy.Matrix.array:c:0_lam2} 1357 | def _idris__123_Python_46_Lib_46_Numpy_46_Matrix_46_array_58_c_58_0_95_lam2_125_( 1358 | in2 1359 | ): 1360 | while True: 1361 | return (65721, in2) # {U_{Python.Lib.Numpy.Matrix.array:c:0_lam1}1} 1362 | 1363 | # Python.Lib.Numpy.Matrix.{array2} 1364 | def _idris_Python_46_Lib_46_Numpy_46_Matrix_46__123_array2_125_(in4, in5, in6): 1365 | while True: 1366 | return _idris_Data_46_Vect_46_foldrImpl(None, None, None, in4, in5, (65672, None), in6) # {U_Prelude.Basics.id1} 1367 | 1368 | # {io_bind2} 1369 | def io_bind2(e0, e1, e2, e3, e4, _idris_w): 1370 | while True: 1371 | return (65725, e0, e1, e2, e3, e4, _idris_w) # {U_{io_bind1}1} 1372 | 1373 | # Main.{main2} 1374 | def _idris_Main_46__123_main2_125_(in6): 1375 | while True: 1376 | return _idris_Prelude_46_Interactive_46_putStr_39_( 1377 | None, 1378 | (_idris_Prelude_46_Show_46_Python_46_Lib_46_Numpy_46_Matrix_46__64_Prelude_46_Show_46_Show_36_Matrix_32_r_32_c_32_dt_58__33_show_58_0( 1379 | None, 1380 | None, 1381 | None, 1382 | None, 1383 | APPLY0( 1384 | APPLY0( 1385 | _idris_Prelude_46_Interfaces_46_Python_46_Lib_46_Numpy_46_Matrix_46__64_Prelude_46_Interfaces_46_Num_36_Matrix_32_r_32_c_32_dt_58__33__43__58_0( 1386 | None, None, None, None 1387 | ), 1388 | APPLY0( 1389 | APPLY0( 1390 | _idris_Prelude_46_Interfaces_46_Python_46_Lib_46_Numpy_46_Matrix_46__64_Prelude_46_Interfaces_46_Num_36_Matrix_32_r_32_c_32_dt_58__33__42__58_0( 1391 | None, None, None, None 1392 | ), 1393 | APPLY0( 1394 | _idris_Python_46_Lib_46_Numpy_46_Matrix_46_fromInteger( 1395 | None, 1396 | (0, u'float', (65661,), (65672, None)), # Python.Lib.Numpy.Matrix.MkDType, {U_Main.{main0}1}, {U_Prelude.Basics.id1} 1397 | 4, 1398 | 4 1399 | ), 1400 | 2 1401 | ) 1402 | ), 1403 | _idris_Python_46_Lib_46_Numpy_46_Matrix_46_transpose( 1404 | None, 1405 | None, 1406 | None, 1407 | None, 1408 | _idris_Python_46_Lib_46_Numpy_46_Matrix_46_dot( 1409 | None, 1410 | None, 1411 | None, 1412 | None, 1413 | None, 1414 | _idris_Main_46_zs(), 1415 | _idris_Main_46_xs() 1416 | ) 1417 | ) 1418 | ) 1419 | ), 1420 | APPLY0( 1421 | _idris_Python_46_Lib_46_Numpy_46_Matrix_46_fromDouble( 1422 | None, 1423 | (0, u'float', (65662,), (65672, None)), # Python.Lib.Numpy.Matrix.MkDType, {U_Main.{main1}1}, {U_Prelude.Basics.id1} 1424 | 4, 1425 | 4 1426 | ), 1427 | 0.2 1428 | ) 1429 | ) 1430 | ) + u'\u000a') 1431 | ) 1432 | 1433 | # {PE_show_408b75993} 1434 | def _idris__123_PE_95_show_95_408b75993_125_(in0): 1435 | while True: 1436 | return _idris_PE_95_Prelude_46_Show_46_List_32_a_32_implementation_32_of_32_Prelude_46_Show_46_Show_44__32_method_32_show_95_d2fe2214( 1437 | in0 1438 | ) 1439 | 1440 | # {Python.Lib.Numpy.Matrix.array:c:0_lam3} 1441 | def _idris__123_Python_46_Lib_46_Numpy_46_Matrix_46_array_58_c_58_0_95_lam3_125_( 1442 | in1 1443 | ): 1444 | while True: 1445 | return (65722,) # {U_{Python.Lib.Numpy.Matrix.array:c:0_lam2}1} 1446 | 1447 | # Python.Lib.Numpy.Matrix.{array3} 1448 | def _idris_Python_46_Lib_46_Numpy_46_Matrix_46__123_array3_125_(in4, in5): 1449 | while True: 1450 | return (65686, in4, in5) # {U_Python.Lib.Numpy.Matrix.{array2}1} 1451 | 1452 | # Main.{main3} 1453 | def _idris_Main_46__123_main3_125_(in5): 1454 | while True: 1455 | return ( 1456 | 65705, # {U_io_bind1} 1457 | None, 1458 | None, 1459 | None, 1460 | _idris_Prelude_46_Interactive_46_putStr_39_( 1461 | None, 1462 | (_idris_Prelude_46_Show_46_Python_46_Lib_46_Numpy_46_Matrix_46__64_Prelude_46_Show_46_Show_36_Matrix_32_r_32_c_32_dt_58__33_show_58_0( 1463 | None, 1464 | None, 1465 | None, 1466 | None, 1467 | _idris_Python_46_Lib_46_Numpy_46_Matrix_46_dot( 1468 | None, 1469 | None, 1470 | None, 1471 | None, 1472 | None, 1473 | _idris_Python_46_Lib_46_Numpy_46_Matrix_46_transpose( 1474 | None, 1475 | None, 1476 | None, 1477 | None, 1478 | _idris_Main_46_xs() 1479 | ), 1480 | _idris_Main_46_xs() 1481 | ) 1482 | ) + u'\u000a') 1483 | ), 1484 | (65663,) # {U_Main.{main2}1} 1485 | ) 1486 | 1487 | # {PE_show_408b75994} 1488 | def _idris__123_PE_95_show_95_408b75994_125_(in2): 1489 | while True: 1490 | return _idris_PE_95_Prelude_46_Show_46_List_32_a_32_implementation_32_of_32_Prelude_46_Show_46_Show_44__32_method_32_showPrec_95_32c264fb( 1491 | None, in2 1492 | ) 1493 | 1494 | # {Python.Lib.Numpy.Matrix.array:c:0_lam4} 1495 | def _idris__123_Python_46_Lib_46_Numpy_46_Matrix_46_array_58_c_58_0_95_lam4_125_( 1496 | in0 1497 | ): 1498 | while True: 1499 | return (65723,) # {U_{Python.Lib.Numpy.Matrix.array:c:0_lam3}1} 1500 | 1501 | # Python.Lib.Numpy.Matrix.{array4} 1502 | def _idris_Python_46_Lib_46_Numpy_46_Matrix_46__123_array4_125_(in4): 1503 | while True: 1504 | return (65687, in4) # {U_Python.Lib.Numpy.Matrix.{array3}1} 1505 | 1506 | # Main.{main4} 1507 | def _idris_Main_46__123_main4_125_(in4): 1508 | while True: 1509 | return ( 1510 | 65705, # {U_io_bind1} 1511 | None, 1512 | None, 1513 | None, 1514 | _idris_Prelude_46_Interactive_46_putStr_39_( 1515 | None, 1516 | (_idris_Prelude_46_Show_46_Python_46_Lib_46_Numpy_46_Matrix_46__64_Prelude_46_Show_46_Show_36_Matrix_32_r_32_c_32_dt_58__33_show_58_0( 1517 | None, 1518 | None, 1519 | None, 1520 | None, 1521 | _idris_Python_46_Lib_46_Numpy_46_Matrix_46_dot( 1522 | None, 1523 | None, 1524 | None, 1525 | None, 1526 | None, 1527 | _idris_Main_46_xs(), 1528 | _idris_Python_46_Lib_46_Numpy_46_Matrix_46_transpose( 1529 | None, 1530 | None, 1531 | None, 1532 | None, 1533 | _idris_Main_46_xs() 1534 | ) 1535 | ) 1536 | ) + u'\u000a') 1537 | ), 1538 | (65664,) # {U_Main.{main3}1} 1539 | ) 1540 | 1541 | # {PE_show_408b75995} 1542 | def _idris__123_PE_95_show_95_408b75995_125_(in1): 1543 | while True: 1544 | return (65718,) # {U_{PE_show_408b75994}1} 1545 | 1546 | # Python.Lib.Numpy.Matrix.{array5} 1547 | def _idris_Python_46_Lib_46_Numpy_46_Matrix_46__123_array5_125_(in3): 1548 | while True: 1549 | return (65688,) # {U_Python.Lib.Numpy.Matrix.{array4}1} 1550 | 1551 | # Main.{main5} 1552 | def _idris_Main_46__123_main5_125_(in3): 1553 | while True: 1554 | return ( 1555 | 65705, # {U_io_bind1} 1556 | None, 1557 | None, 1558 | None, 1559 | _idris_Prelude_46_Interactive_46_putStr_39_( 1560 | None, 1561 | (_idris_Prelude_46_Show_46_Python_46_Lib_46_Numpy_46_Matrix_46__64_Prelude_46_Show_46_Show_36_Matrix_32_r_32_c_32_dt_58__33_show_58_0( 1562 | None, 1563 | None, 1564 | None, 1565 | None, 1566 | _idris_Python_46_Lib_46_Numpy_46_Matrix_46_dot( 1567 | None, 1568 | None, 1569 | None, 1570 | None, 1571 | None, 1572 | _idris_Main_46_xs(), 1573 | _idris_Main_46_zs() 1574 | ) 1575 | ) + u'\u000a') 1576 | ), 1577 | (65665,) # {U_Main.{main4}1} 1578 | ) 1579 | 1580 | # Python.Lib.Numpy.Matrix.{array6} 1581 | def _idris_Python_46_Lib_46_Numpy_46_Matrix_46__123_array6_125_(in2): 1582 | while True: 1583 | return (65689,) # {U_Python.Lib.Numpy.Matrix.{array5}1} 1584 | 1585 | # Main.{main6} 1586 | def _idris_Main_46__123_main6_125_(in2): 1587 | while True: 1588 | return ( 1589 | 65705, # {U_io_bind1} 1590 | None, 1591 | None, 1592 | None, 1593 | _idris_Prelude_46_Interactive_46_putStr_39_( 1594 | None, 1595 | (_idris_Prelude_46_Show_46_Python_46_Lib_46_Numpy_46_Matrix_46__64_Prelude_46_Show_46_Show_36_Matrix_32_r_32_c_32_dt_58__33_show_58_0( 1596 | None, 1597 | None, 1598 | None, 1599 | None, 1600 | _idris_Main_46_zs() 1601 | ) + u'\u000a') 1602 | ), 1603 | (65666,) # {U_Main.{main5}1} 1604 | ) 1605 | 1606 | # Main.{main7} 1607 | def _idris_Main_46__123_main7_125_(in1): 1608 | while True: 1609 | return ( 1610 | 65705, # {U_io_bind1} 1611 | None, 1612 | None, 1613 | None, 1614 | _idris_Prelude_46_Interactive_46_putStr_39_( 1615 | None, 1616 | (_idris_Prelude_46_Show_46_Python_46_Lib_46_Numpy_46_Matrix_46__64_Prelude_46_Show_46_Show_36_Matrix_32_r_32_c_32_dt_58__33_show_58_0( 1617 | None, 1618 | None, 1619 | None, 1620 | None, 1621 | _idris_Main_46_ys() 1622 | ) + u'\u000a') 1623 | ), 1624 | (65667,) # {U_Main.{main6}1} 1625 | ) 1626 | 1627 | # Main.{main8} 1628 | def _idris_Main_46__123_main8_125_(in0): 1629 | while True: 1630 | return ( 1631 | 65705, # {U_io_bind1} 1632 | None, 1633 | None, 1634 | None, 1635 | _idris_Prelude_46_Interactive_46_putStr_39_( 1636 | None, 1637 | (_idris_Prelude_46_Show_46_Python_46_Lib_46_Numpy_46_Matrix_46__64_Prelude_46_Show_46_Show_36_Matrix_32_r_32_c_32_dt_58__33_show_58_0( 1638 | None, 1639 | None, 1640 | None, 1641 | None, 1642 | _idris_Main_46_xs() 1643 | ) + u'\u000a') 1644 | ), 1645 | (65668,) # {U_Main.{main7}1} 1646 | ) 1647 | 1648 | # Python.Lib.Numpy.Matrix.array, c 1649 | def _idris_Python_46_Lib_46_Numpy_46_Matrix_46_array_58_c_58_0( 1650 | e0, e1, e2, e3, e4, e5, e6, e7 1651 | ): 1652 | while True: 1653 | return _idris_Python_46_Prim_46_pyList( 1654 | None, 1655 | APPLY0(_idris_Prelude_46_List_46_toList(None, None, (65724,)), e7) # {U_{Python.Lib.Numpy.Matrix.array:c:0_lam4}1} 1656 | ) 1657 | 1658 | # Decidable.Equality.Decidable.Equality.Char implementation of Decidable.Equality.DecEq, method decEq, primitiveNotEq 1659 | def _idris_Decidable_46_Equality_46_Decidable_46_Equality_46__64_Decidable_46_Equality_46_DecEq_36_Char_58__33_decEq_58_0_58_primitiveNotEq_58_0(): 1660 | while True: 1661 | return None 1662 | 1663 | # Decidable.Equality.Decidable.Equality.Int implementation of Decidable.Equality.DecEq, method decEq, primitiveNotEq 1664 | def _idris_Decidable_46_Equality_46_Decidable_46_Equality_46__64_Decidable_46_Equality_46_DecEq_36_Int_58__33_decEq_58_0_58_primitiveNotEq_58_0(): 1665 | while True: 1666 | return None 1667 | 1668 | # Decidable.Equality.Decidable.Equality.Integer implementation of Decidable.Equality.DecEq, method decEq, primitiveNotEq 1669 | def _idris_Decidable_46_Equality_46_Decidable_46_Equality_46__64_Decidable_46_Equality_46_DecEq_36_Integer_58__33_decEq_58_0_58_primitiveNotEq_58_0(): 1670 | while True: 1671 | return None 1672 | 1673 | # Decidable.Equality.Decidable.Equality.ManagedPtr implementation of Decidable.Equality.DecEq, method decEq, primitiveNotEq 1674 | def _idris_Decidable_46_Equality_46_Decidable_46_Equality_46__64_Decidable_46_Equality_46_DecEq_36_ManagedPtr_58__33_decEq_58_0_58_primitiveNotEq_58_0(): 1675 | while True: 1676 | return None 1677 | 1678 | # Decidable.Equality.Decidable.Equality.Ptr implementation of Decidable.Equality.DecEq, method decEq, primitiveNotEq 1679 | def _idris_Decidable_46_Equality_46_Decidable_46_Equality_46__64_Decidable_46_Equality_46_DecEq_36_Ptr_58__33_decEq_58_0_58_primitiveNotEq_58_0(): 1680 | while True: 1681 | return None 1682 | 1683 | # Decidable.Equality.Decidable.Equality.String implementation of Decidable.Equality.DecEq, method decEq, primitiveNotEq 1684 | def _idris_Decidable_46_Equality_46_Decidable_46_Equality_46__64_Decidable_46_Equality_46_DecEq_36_String_58__33_decEq_58_0_58_primitiveNotEq_58_0(): 1685 | while True: 1686 | return None 1687 | 1688 | # Prelude.Show.Prelude.Show.List a implementation of Prelude.Show.Show, method show, show' 1689 | def _idris_Prelude_46_Show_46_Prelude_46_Show_46__64_Prelude_46_Show_46_Show_36_List_32_a_58__33_show_58_0_58_show_39__58_0( 1690 | e0, e1, e2, e3, e4 1691 | ): 1692 | while True: 1693 | if e4: # Prelude.List.:: 1694 | in0, in1 = e4.head, e4.tail 1695 | if not in1: # Prelude.List.Nil 1696 | return (e3 + APPLY0(_idris_Prelude_46_Show_46_show(None, e2), in0)) 1697 | else: 1698 | e0, e1, e2, e3, e4, = None, None, e2, (e3 + (APPLY0(_idris_Prelude_46_Show_46_show(None, e2), in0) + u', ')), in1, 1699 | continue 1700 | return _idris_error("unreachable due to tail call") 1701 | return _idris_error("unreachable due to case in tail position") 1702 | else: # Prelude.List.Nil 1703 | return e3 1704 | return _idris_error("unreachable due to case in tail position") 1705 | 1706 | # Prelude.Interfaces.Prelude.Nat.Nat implementation of Prelude.Interfaces.Eq, method == 1707 | def _idris_Prelude_46_Interfaces_46_Prelude_46_Nat_46__64_Prelude_46_Interfaces_46_Eq_36_Nat_58__33__61__61__58_0( 1708 | e0, e1 1709 | ): 1710 | while True: 1711 | if e1 == 0: 1712 | if e0 == 0: 1713 | return True 1714 | else: 1715 | return False 1716 | return _idris_error("unreachable due to case in tail position") 1717 | elif True: 1718 | in0 = (e1 - 1) 1719 | if e0 == 0: 1720 | return False 1721 | else: 1722 | in1 = (e0 - 1) 1723 | e0, e1, = in1, in0, 1724 | continue 1725 | return _idris_error("unreachable due to tail call") 1726 | return _idris_error("unreachable due to case in tail position") 1727 | else: 1728 | return False 1729 | return _idris_error("unreachable due to case in tail position") 1730 | 1731 | # Prelude.Interfaces.Prelude.Show.Prec implementation of Prelude.Interfaces.Eq, method == 1732 | def _idris_Prelude_46_Interfaces_46_Prelude_46_Show_46__64_Prelude_46_Interfaces_46_Eq_36_Prec_58__33__61__61__58_0( 1733 | e0, e1 1734 | ): 1735 | while True: 1736 | if e1[0] == 4: # Prelude.Show.User 1737 | in0 = e1[1] 1738 | if e0[0] == 4: # Prelude.Show.User 1739 | in1 = e0[1] 1740 | return _idris_Prelude_46_Interfaces_46_Prelude_46_Nat_46__64_Prelude_46_Interfaces_46_Eq_36_Nat_58__33__61__61__58_0( 1741 | in1, in0 1742 | ) 1743 | else: 1744 | aux1 = (_idris_Prelude_46_Show_46_precCon(e0) == _idris_Prelude_46_Show_46_precCon(e1)) 1745 | if aux1 == 0: 1746 | return False 1747 | else: 1748 | return True 1749 | return _idris_error("unreachable due to case in tail position") 1750 | return _idris_error("unreachable due to case in tail position") 1751 | else: 1752 | aux2 = (_idris_Prelude_46_Show_46_precCon(e0) == _idris_Prelude_46_Show_46_precCon(e1)) 1753 | if aux2 == 0: 1754 | return False 1755 | else: 1756 | return True 1757 | return _idris_error("unreachable due to case in tail position") 1758 | return _idris_error("unreachable due to case in tail position") 1759 | 1760 | # Prelude.Functor.Prelude.Monad.IO' ffi implementation of Prelude.Functor.Functor, method map 1761 | def _idris_Prelude_46_Functor_46_Prelude_46_Monad_46__64_Prelude_46_Functor_46_Functor_36_IO_39__32_ffi_58__33_map_58_0( 1762 | e0, e1, e2, e3, e4 1763 | ): 1764 | while True: 1765 | return (65705, None, None, None, e4, (65673, e3)) # {U_io_bind1}, {U_Prelude.Functor.{Prelude.Monad.@Prelude.Functor.Functor$IO' ffi:!map:0_lam0}1} 1766 | 1767 | # Prelude.Functor.Data.Vect.Vect n implementation of Prelude.Functor.Functor, method map 1768 | def _idris_Prelude_46_Functor_46_Data_46_Vect_46__64_Prelude_46_Functor_46_Functor_36_Vect_32_n_58__33_map_58_0( 1769 | e0, e1, e2, e3, e4 1770 | ): 1771 | while True: 1772 | if e4[0] == 1: # Data.Vect.:: 1773 | in0, in1 = e4[1:] 1774 | return ( 1775 | 1, # Data.Vect.:: 1776 | APPLY0(e3, in0), 1777 | _idris_Prelude_46_Functor_46_Data_46_Vect_46__64_Prelude_46_Functor_46_Functor_36_Vect_32_n_58__33_map_58_0( 1778 | None, None, None, e3, in1 1779 | ) 1780 | ) 1781 | else: # Data.Vect.Nil 1782 | return (0,) # Data.Vect.Nil 1783 | return _idris_error("unreachable due to case in tail position") 1784 | 1785 | # Prelude.Interfaces.Python.Lib.Numpy.Matrix.Matrix r c dt implementation of Prelude.Interfaces.Num, method * 1786 | def _idris_Prelude_46_Interfaces_46_Python_46_Lib_46_Numpy_46_Matrix_46__64_Prelude_46_Interfaces_46_Num_36_Matrix_32_r_32_c_32_dt_58__33__42__58_0( 1787 | e0, e1, e2, e3 1788 | ): 1789 | while True: 1790 | return (65727, None, None, None, None, u'__mul__', None) # {U_Python.Lib.Numpy.Matrix.op2} 1791 | 1792 | # Prelude.Interfaces.Python.Lib.Numpy.Matrix.Matrix r c dt implementation of Prelude.Interfaces.Num, method + 1793 | def _idris_Prelude_46_Interfaces_46_Python_46_Lib_46_Numpy_46_Matrix_46__64_Prelude_46_Interfaces_46_Num_36_Matrix_32_r_32_c_32_dt_58__33__43__58_0( 1794 | e0, e1, e2, e3 1795 | ): 1796 | while True: 1797 | return (65727, None, None, None, None, u'__add__', None) # {U_Python.Lib.Numpy.Matrix.op2} 1798 | 1799 | # Prelude.Interfaces.Prelude.Interfaces.Integer implementation of Prelude.Interfaces.Ord, method compare 1800 | def _idris_Prelude_46_Interfaces_46_Prelude_46_Interfaces_46__64_Prelude_46_Interfaces_46_Ord_36_Integer_58__33_compare_58_0( 1801 | e0, e1 1802 | ): 1803 | while True: 1804 | aux2 = (e0 == e1) 1805 | if aux2 == 0: 1806 | aux3 = False 1807 | else: 1808 | aux3 = True 1809 | aux1 = aux3 1810 | if not aux1: # Prelude.Bool.False 1811 | aux5 = (e0 < e1) 1812 | if aux5 == 0: 1813 | aux6 = False 1814 | else: 1815 | aux6 = True 1816 | aux4 = aux6 1817 | if not aux4: # Prelude.Bool.False 1818 | return (2,) # Prelude.Interfaces.GT 1819 | else: # Prelude.Bool.True 1820 | return (0,) # Prelude.Interfaces.LT 1821 | return _idris_error("unreachable due to case in tail position") 1822 | else: # Prelude.Bool.True 1823 | return (1,) # Prelude.Interfaces.EQ 1824 | return _idris_error("unreachable due to case in tail position") 1825 | 1826 | # Prelude.Interfaces.Prelude.Nat.Nat implementation of Prelude.Interfaces.Ord, method compare 1827 | def _idris_Prelude_46_Interfaces_46_Prelude_46_Nat_46__64_Prelude_46_Interfaces_46_Ord_36_Nat_58__33_compare_58_0( 1828 | e0, e1 1829 | ): 1830 | while True: 1831 | if e1 == 0: 1832 | if e0 == 0: 1833 | return (1,) # Prelude.Interfaces.EQ 1834 | else: 1835 | in0 = (e0 - 1) 1836 | return (2,) # Prelude.Interfaces.GT 1837 | return _idris_error("unreachable due to case in tail position") 1838 | else: 1839 | in1 = (e1 - 1) 1840 | if e0 == 0: 1841 | return (0,) # Prelude.Interfaces.LT 1842 | else: 1843 | in2 = (e0 - 1) 1844 | e0, e1, = in2, in1, 1845 | continue 1846 | return _idris_error("unreachable due to tail call") 1847 | return _idris_error("unreachable due to case in tail position") 1848 | return _idris_error("unreachable due to case in tail position") 1849 | 1850 | # Prelude.Interfaces.Prelude.Show.Prec implementation of Prelude.Interfaces.Ord, method >= 1851 | def _idris_Prelude_46_Interfaces_46_Prelude_46_Show_46__64_Prelude_46_Interfaces_46_Ord_36_Prec_58__33__62__61__58_0( 1852 | e0, e1 1853 | ): 1854 | while True: 1855 | aux2 = _idris_Prelude_46_Interfaces_46_Prelude_46_Show_46__64_Prelude_46_Interfaces_46_Ord_36_Prec_58__33_compare_58_0( 1856 | e0, e1 1857 | ) 1858 | if aux2[0] == 2: # Prelude.Interfaces.GT 1859 | aux3 = True 1860 | else: 1861 | aux3 = False 1862 | aux1 = aux3 1863 | if not aux1: # Prelude.Bool.False 1864 | return _idris_Prelude_46_Interfaces_46__123_Prelude_46_Show_46__64_Prelude_46_Interfaces_46_Ord_36_Prec_58__33__62__61__58_0_95_lam0_125_( 1865 | e0, e1 1866 | ) 1867 | else: # Prelude.Bool.True 1868 | return True 1869 | return _idris_error("unreachable due to case in tail position") 1870 | 1871 | # Prelude.Interfaces.Prelude.Show.Prec implementation of Prelude.Interfaces.Ord, method compare 1872 | def _idris_Prelude_46_Interfaces_46_Prelude_46_Show_46__64_Prelude_46_Interfaces_46_Ord_36_Prec_58__33_compare_58_0( 1873 | e0, e1 1874 | ): 1875 | while True: 1876 | if e1[0] == 4: # Prelude.Show.User 1877 | in0 = e1[1] 1878 | if e0[0] == 4: # Prelude.Show.User 1879 | in1 = e0[1] 1880 | return _idris_Prelude_46_Interfaces_46_Prelude_46_Nat_46__64_Prelude_46_Interfaces_46_Ord_36_Nat_58__33_compare_58_0( 1881 | in1, in0 1882 | ) 1883 | else: 1884 | return _idris_Prelude_46_Interfaces_46_Prelude_46_Interfaces_46__64_Prelude_46_Interfaces_46_Ord_36_Integer_58__33_compare_58_0( 1885 | _idris_Prelude_46_Show_46_precCon(e0), 1886 | _idris_Prelude_46_Show_46_precCon(e1) 1887 | ) 1888 | return _idris_error("unreachable due to case in tail position") 1889 | else: 1890 | return _idris_Prelude_46_Interfaces_46_Prelude_46_Interfaces_46__64_Prelude_46_Interfaces_46_Ord_36_Integer_58__33_compare_58_0( 1891 | _idris_Prelude_46_Show_46_precCon(e0), 1892 | _idris_Prelude_46_Show_46_precCon(e1) 1893 | ) 1894 | return _idris_error("unreachable due to case in tail position") 1895 | 1896 | # Prelude.Show.Prelude.Show.Bool implementation of Prelude.Show.Show, method show 1897 | def _idris_Prelude_46_Show_46_Prelude_46_Show_46__64_Prelude_46_Show_46_Show_36_Bool_58__33_show_58_0( 1898 | e0 1899 | ): 1900 | while True: 1901 | if not e0: # Prelude.Bool.False 1902 | return u'False' 1903 | else: # Prelude.Bool.True 1904 | return u'True' 1905 | return _idris_error("unreachable due to case in tail position") 1906 | 1907 | # Prelude.Show.Python.Lib.Numpy.Matrix.Matrix r c dt implementation of Prelude.Show.Show, method show 1908 | def _idris_Prelude_46_Show_46_Python_46_Lib_46_Numpy_46_Matrix_46__64_Prelude_46_Show_46_Show_36_Matrix_32_r_32_c_32_dt_58__33_show_58_0( 1909 | e0, e1, e2, e3, e4 1910 | ): 1911 | while True: 1912 | return _idris_unsafePerformIO( 1913 | None, 1914 | None, 1915 | _idris_Python_46_Functions_46__36__46_( 1916 | None, 1917 | None, 1918 | (0,), # Python.Telescope.Return 1919 | _idris_Python_46_Fields_46__47__46_(None, None, e4, u'__str__', None), 1920 | None, 1921 | Unit 1922 | ) 1923 | ) 1924 | 1925 | # Prelude.Show.Prelude.Show.Maybe a implementation of Prelude.Show.Show, method showPrec 1926 | def _idris_Prelude_46_Show_46_Prelude_46_Show_46__64_Prelude_46_Show_46_Show_36_Maybe_32_a_58__33_showPrec_58_0( 1927 | e0, e1, e2, e3 1928 | ): 1929 | while True: 1930 | if e3 is not None: # Prelude.Maybe.Just 1931 | in0 = e3 1932 | aux1 = _idris_Prelude_46_Interfaces_46_Prelude_46_Show_46__64_Prelude_46_Interfaces_46_Ord_36_Prec_58__33__62__61__58_0( 1933 | e2, (6,) # Prelude.Show.App 1934 | ) 1935 | if not aux1: # Prelude.Bool.False 1936 | return (u'Just' + (u' ' + APPLY0(APPLY0(_idris_Prelude_46_Show_46_showPrec(None, e1), None), in0))) 1937 | else: # Prelude.Bool.True 1938 | return (u'(' + ((u'Just' + (u' ' + APPLY0(APPLY0(_idris_Prelude_46_Show_46_showPrec(None, e1), None), in0))) + u')')) 1939 | return _idris_error("unreachable due to case in tail position") 1940 | else: # Prelude.Maybe.Nothing 1941 | return u'Nothing' 1942 | return _idris_error("unreachable due to case in tail position") 1943 | 1944 | # with block in Prelude.Interfaces.Prelude.Show.Prec implementation of Prelude.Interfaces.Ord, method > 1945 | def _idris__95_Prelude_46_Interfaces_46_Prelude_46_Show_46__64_Prelude_46_Interfaces_46_Ord_36_Prec_58__33__62__58_0_95_with_95_27( 1946 | e0, e1, e2 1947 | ): 1948 | while True: 1949 | if e0[0] == 2: # Prelude.Interfaces.GT 1950 | return True 1951 | else: 1952 | return False 1953 | return _idris_error("unreachable due to case in tail position") 1954 | 1955 | # case block in io_bind at IO.idr:107:34 1956 | def _idris_io_95_bind_95_IO_95__95_idr_95_107_95_34_95_case( 1957 | e0, e1, e2, e3, e4, e5, e6, e7 1958 | ): 1959 | while True: 1960 | return APPLY0(e7, e5) 1961 | 1962 | # case block in Void at (casefun Void) 1963 | def _idris_Void_95__95__95_casefun_95__95_Void_95__95__95_case(): 1964 | while True: 1965 | return None 1966 | 1967 | # <> 1968 | def _idris_Void_95_elim(): 1969 | while True: 1970 | return None 1971 | 1972 | if __name__ == '__main__': 1973 | runMain0() 1974 | -------------------------------------------------------------------------------- /examples/pythag.expected.txt: -------------------------------------------------------------------------------- 1 | [(3, (4, 5)), (6, (8, 10)), (5, (12, 13)), (9, (12, 15)), (8, (15, 17)), (12, (16, 20)), (15, (20, 25)), (7, (24, 25)), (10, (24, 26)), (20, (21, 29)), (18, (24, 30)), (16, (30, 34)), (21, (28, 35)), (12, (35, 37)), (15, (36, 39)), (24, (32, 40)), (9, (40, 41)), (27, (36, 45)), (30, (40, 50)), (14, (48, 50)), (24, (45, 51)), (20, (48, 52)), (28, (45, 53)), (33, (44, 55)), (40, (42, 58)), (36, (48, 60)), (11, (60, 61)), (39, (52, 65)), (33, (56, 65)), (25, (60, 65)), (16, (63, 65)), (32, (60, 68)), (42, (56, 70)), (48, (55, 73)), (24, (70, 74)), (45, (60, 75)), (21, (72, 75)), (30, (72, 78)), (48, (64, 80)), (18, (80, 82)), (51, (68, 85)), (40, (75, 85)), (36, (77, 85)), (13, (84, 85)), (60, (63, 87)), (39, (80, 89)), (54, (72, 90)), (35, (84, 91)), (57, (76, 95)), (65, (72, 97)), (60, (80, 100)), (28, (96, 100))] 2 | ["bar", "baz", "foo"] 3 | -------------------------------------------------------------------------------- /examples/pythag.idr: -------------------------------------------------------------------------------- 1 | -- Shamelessly stolen from Edwin Brady 2 | module Main 3 | 4 | pythag : Int -> List (Int, Int, Int) 5 | pythag max = [ 6 | (x, y, z) 7 | | z <- [1..max] 8 | , y <- [1..z] 9 | , x <- [1..y] 10 | , x * x + y *y == z * z 11 | ] 12 | 13 | main : IO () 14 | main = do 15 | printLn $ pythag 100 16 | printLn $ sort ["foo", "bar", "baz"] 17 | -------------------------------------------------------------------------------- /idris-python.cabal: -------------------------------------------------------------------------------- 1 | Name: idris-python 2 | Version: 0.0.0.1 3 | License: BSD3 4 | License-file: LICENSE 5 | Author: Matus Tejiscak 6 | Maintainer: Matus Tejiscak 7 | Build-Type: Simple 8 | Cabal-Version: >= 1.8 9 | 10 | Executable idris-codegen-python 11 | Main-is: Main.hs 12 | hs-source-dirs: src 13 | 14 | Build-depends: idris >= 1.1 && < 1.2 15 | , base 16 | , containers 17 | , directory 18 | , filepath 19 | , haskeline >= 0.7 20 | , mtl 21 | , transformers 22 | , text 23 | 24 | other-modules: IRTS.CodegenPython 25 | 26 | if os(linux) 27 | cpp-options: -DLINUX 28 | build-depends: unix < 2.8 29 | if os(freebsd) 30 | cpp-options: -DFREEBSD 31 | build-depends: unix < 2.8 32 | if os(dragonfly) 33 | cpp-options: -DDRAGONFLY 34 | build-depends: unix < 2.8 35 | if os(darwin) 36 | cpp-options: -DMACOSX 37 | build-depends: unix < 2.8 38 | if os(windows) 39 | cpp-options: -DWINDOWS 40 | build-depends: Win32 < 2.4 41 | 42 | ghc-prof-options: -auto-all -caf-all 43 | ghc-options: -threaded -rtsopts -funbox-strict-fields 44 | 45 | 46 | -------------------------------------------------------------------------------- /lib/Python.idr: -------------------------------------------------------------------------------- 1 | module Python 2 | 3 | import public Python.Telescope 4 | import public Python.Objects 5 | import public Python.IO 6 | import public Python.Fields 7 | import public Python.Functions 8 | import public Python.Exceptions 9 | 10 | %default total 11 | %access public export 12 | 13 | ||| Import a Python module. This is a low-level function 14 | ||| since the correctness of signatures cannot be checked. 15 | ||| 16 | ||| Libraries are encouraged to provide an ``import_`` function in their namespaces. 17 | ||| 18 | ||| @ sig Signature of the returned object. Not checked. 19 | ||| @ modName Name of the module, as given to ``__import__``. 20 | export 21 | importModule : (modName : String) -> PIO (Obj sig) 22 | importModule {sig = sig} modName = 23 | foreign FFI_Py "_idris_pymodule" (String -> PIO (Obj sig)) modName 24 | 25 | ||| Turn a PIO action into a Python function. 26 | ||| The function can then be used as a target for threading.Thread etc. 27 | export 28 | marshalPIO : PIO a -> [] ~> a 29 | marshalPIO {a = a} action = 30 | unsafePerformIO $ 31 | foreign FFI_Py "_idris_marshal_PIO" (Raw (PIO a) -> PIO ([] ~> a)) (MkRaw action) 32 | 33 | export 34 | getGlobal : (name : String) -> Obj sig 35 | getGlobal {sig=sig} name = 36 | unsafePerformIO $ 37 | foreign FFI_Py "_idris_get_global" (String -> PIO (Obj sig)) name 38 | -------------------------------------------------------------------------------- /lib/Python/Exceptions.idr: -------------------------------------------------------------------------------- 1 | module Python.Exceptions 2 | 3 | import Python.Objects 4 | import Python.Fields 5 | import Python.IO 6 | 7 | %default total 8 | %access public export 9 | 10 | Exception : Signature 11 | Exception f = case f of 12 | "message" => Attr String 13 | _ => Object f 14 | 15 | ||| Standard Python exceptions. 16 | data ExceptionType : Type where 17 | StopIteration : ExceptionType 18 | StandardError : ExceptionType 19 | BufferError : ExceptionType 20 | ArithmeticError : ExceptionType 21 | FloatingPointError : ExceptionType 22 | OverflowError : ExceptionType 23 | ZeroDivisionError : ExceptionType 24 | AssertionError : ExceptionType 25 | AttributeError : ExceptionType 26 | EnvironmentError : ExceptionType 27 | IOError : ExceptionType 28 | OSError : ExceptionType 29 | WindowsError : ExceptionType 30 | VMSError : ExceptionType 31 | EOFError : ExceptionType 32 | ImportError : ExceptionType 33 | LookupError : ExceptionType 34 | IndexError : ExceptionType 35 | KeyError : ExceptionType 36 | MemoryError : ExceptionType 37 | NameError : ExceptionType 38 | UnboundLocalError : ExceptionType 39 | ReferenceError : ExceptionType 40 | RuntimeError : ExceptionType 41 | NotImplementedError : ExceptionType 42 | SyntaxError : ExceptionType 43 | IndentationError : ExceptionType 44 | TabError : ExceptionType 45 | SystemError : ExceptionType 46 | TypeError : ExceptionType 47 | ValueError : ExceptionType 48 | UnicodeError : ExceptionType 49 | UnicodeDecodeError : ExceptionType 50 | UnicodeEncodeError : ExceptionType 51 | UnicodeTranslateError : ExceptionType 52 | Other : String -> ExceptionType 53 | 54 | private 55 | fromString : String -> ExceptionType 56 | fromString s = case s of 57 | "StopIteration" => StopIteration 58 | "StandardError" => StandardError 59 | "BufferError" => BufferError 60 | "ArithmeticError" => ArithmeticError 61 | "FloatingPointError" => FloatingPointError 62 | "OverflowError" => OverflowError 63 | "ZeroDivisionError" => ZeroDivisionError 64 | "AssertionError" => AssertionError 65 | "AttributeError" => AttributeError 66 | "EnvironmentError" => EnvironmentError 67 | "IOError" => IOError 68 | "OSError" => OSError 69 | "WindowsError" => WindowsError 70 | "VMSError" => VMSError 71 | "EOFError" => EOFError 72 | "ImportError" => ImportError 73 | "LookupError" => LookupError 74 | "IndexError" => IndexError 75 | "KeyError" => KeyError 76 | "MemoryError" => MemoryError 77 | "NameError" => NameError 78 | "UnboundLocalError" => UnboundLocalError 79 | "ReferenceError" => ReferenceError 80 | "RuntimeError" => RuntimeError 81 | "NotImplementedError" => NotImplementedError 82 | "SyntaxError" => SyntaxError 83 | "IndentationError" => IndentationError 84 | "TabError" => TabError 85 | "SystemError" => SystemError 86 | "TypeError" => TypeError 87 | "ValueError" => ValueError 88 | "UnicodeError" => UnicodeError 89 | "UnicodeDecodeError" => UnicodeDecodeError 90 | "UnicodeEncodeError" => UnicodeEncodeError 91 | "UnicodeTranslateError" => UnicodeTranslateError 92 | other => Other other 93 | 94 | ||| Result of try-catch. 95 | data Result : Type -> Type where 96 | ||| No exception was raised, `PIO` action was performed normally. 97 | OK : (x : a) -> Result a 98 | 99 | ||| An exception was raised. 100 | Except : (etype : ExceptionType) -> (e : Obj Exception) -> Result a 101 | 102 | ||| Catch exceptions in the given PIO action. 103 | export 104 | try : PIO a -> PIO (Result a) 105 | try {a = a} x = do 106 | MkRaw r <- foreign 107 | FFI_Py 108 | "_idris_try" 109 | (Raw (PIO a) 110 | -> (Obj Exception -> Raw (Either (Obj Exception) a)) 111 | -> (Raw a -> Raw (Either (Obj Exception) a)) 112 | -> PIO (Raw $ Either (Obj Exception) a) 113 | ) 114 | (MkRaw x) 115 | (MkRaw . Left) 116 | (MkRaw . Right . unRaw) 117 | 118 | case r of 119 | Right x => pure $ OK x 120 | Left e => do 121 | let et = e /. "__class__" /. "__name__" 122 | pure $ Except (fromString et) e 123 | 124 | export 125 | raise : Obj Exception -> PIO a 126 | raise {a = a} e = unRaw <$> foreign FFI_Py "_idris_raise" (Obj Exception -> PIO (Raw a)) e 127 | 128 | catch : PIO (Result a) -> (ExceptionType -> Obj Exception -> PIO a) -> PIO a 129 | catch action handler = do 130 | OK result <- action 131 | | Except etype e => handler etype e 132 | pure result 133 | 134 | ||| Get basic information about the exception as `String`. 135 | export 136 | showException : Obj Exception -> String 137 | showException e = 138 | unsafePerformIO 139 | $ foreign FFI_Py "str" (Obj Exception -> PIO String) e 140 | 141 | {- 142 | instance Show (Obj Exception) where 143 | show = showException 144 | -} 145 | -------------------------------------------------------------------------------- /lib/Python/Fields.idr: -------------------------------------------------------------------------------- 1 | module Python.Fields 2 | 3 | import Python.Objects 4 | import Python.IO 5 | 6 | import Language.Reflection 7 | 8 | %default total 9 | %access public export 10 | %language ErrorReflection 11 | 12 | infixl 4 /. 13 | ||| Attribute accessor. 14 | ||| 15 | ||| @ obj Obj with the given signature. 16 | ||| @ f Name of the requested field. 17 | (/.) : 18 | (obj : Obj sig) -> (f : String) 19 | -> {auto pf : sig f = Attr t} 20 | -> t 21 | (/.) {t = t} (MkObj obj) f = 22 | unRaw . unsafePerformIO $ 23 | foreign FFI_Py "getattr" (Ptr -> String -> PIO (Raw t)) obj f 24 | 25 | data FieldParams : Type -> Type where 26 | FP : String -> .(params : pt) -> FieldParams pt 27 | 28 | fpName : FieldParams pt -> String 29 | fpName (FP n ps) = n 30 | 31 | fpParams : FieldParams pt -> pt 32 | fpParams (FP n ps) = ps 33 | 34 | infixl 4 //. 35 | (//.) : 36 | (obj : Obj sig) -> (fps : FieldParams pt) 37 | -> {auto pf : sig (fpName fps) = PAttr pt tf} 38 | -> tf (fpParams fps) 39 | (//.) {tf=tf} (MkObj obj) (FP f ps) = 40 | unRaw . unsafePerformIO $ 41 | foreign FFI_Py "getattr" (Ptr -> String -> PIO (Raw $ tf ps)) obj f 42 | 43 | infixl 4 /: 44 | ||| Attribute accessor, useful for chaining. 45 | ||| 46 | ||| @ obj PIO action returning an object. 47 | ||| @ f Name of the requested field. 48 | (/:) : 49 | (obj : PIO (Obj sig)) -> (f : String) 50 | -> {auto pf : sig f = Attr t} 51 | -> PIO t 52 | (/:) obj f = (/. f) <$> obj 53 | 54 | infixl 4 //: 55 | (//:) : 56 | (obj : PIO (Obj sig)) -> (fps : FieldParams pt) 57 | -> {auto pf : sig (fpName fps) = PAttr pt tf} 58 | -> PIO (tf (fpParams fps)) 59 | (//:) obj fps = (//. fps) <$> obj 60 | 61 | -- Error reflection for better error messages. 62 | -- TODO: generalise this 63 | fieldErr : Err -> Maybe (List ErrorReportPart) 64 | {- 65 | fieldErr (CantSolveGoal `(~(App sig fn) = PAttr Unit ~(Bind v (Lam _) ty)) ntms) 66 | = Just 67 | [ TextPart "Field", TermPart fn 68 | , TextPart "does not exist in object signature", TermPart sig 69 | , TextPart "with the type", TermPart ty, TextPart "(or at all)." 70 | ] 71 | fieldErr (CantSolveGoal `(~(App sig fn) = ~rhs) ntms) 72 | = Just 73 | [ TextPart "Field", TermPart fn 74 | , TextPart "does not exist in object signature", TermPart sig 75 | , TextPart "with the correct type (or at all)." 76 | ] 77 | -} 78 | fieldErr (CantSolveGoal `(PAttr Unit ~(Bind _ (Lam _) tyl) = PAttr Unit ~(Bind _ (Lam _) tyr)) ntms) 79 | = Just 80 | [ TextPart "Cannot match field type", TermPart tyl 81 | , TextPart "with context-required type", TermPart tyr 82 | ] 83 | 84 | fieldErr (CantSolveGoal `(NotField = ~rhs) ntms) 85 | = Just 86 | [ TextPart "The requested field does not exist." 87 | ] 88 | fieldErr _ = Nothing 89 | 90 | %error_handlers Python.Fields.(/.) pf fieldErr 91 | %error_handlers Python.Fields.(//.) pf fieldErr 92 | %error_handlers Python.Fields.(/:) pf fieldErr 93 | %error_handlers Python.Fields.(//:) pf fieldErr 94 | -------------------------------------------------------------------------------- /lib/Python/Functions.idr: -------------------------------------------------------------------------------- 1 | module Python.Functions 2 | 3 | import Python.Objects 4 | import Python.Fields 5 | import Python.Telescope 6 | import Python.IO 7 | 8 | %default total 9 | %access public export 10 | 11 | Function : (t : Telescope a) -> Signature 12 | Function {a = a} t f = case f of 13 | "__call__" => Call {a = a} t 14 | _ => Object f 15 | 16 | infix 5 ~> 17 | ||| Infix alias for functions with fixed arguments. 18 | (~>) : List Type -> Type -> Type 19 | (~>) args ret = Obj . Function $ simple args ret 20 | 21 | infix 5 ~~> 22 | (~~>) : List Type -> Type -> Field 23 | (~~>) args ret = Attr $ args ~> ret 24 | 25 | fun : (t : Telescope a) -> Field 26 | fun t = Attr . Obj $ Function t 27 | 28 | ||| Strip the given tuple `xs` to the list of runtime-relevant values. 29 | strip : (t : Telescope a) -> (args : a) -> List Dyn 30 | strip (Return _) () = [] 31 | strip (Bind (Pi _ ) tf) (x ** xs) = toDyn x :: strip (tf x) xs 32 | strip (Bind (Forall _ ) tf) (x ** xs) = strip (tf x) xs 33 | strip (Bind (Default _ d) tf) (x ** xs) = toDyn x :: strip (tf $ fromMaybe d x) xs 34 | 35 | infixl 4 $. 36 | ||| Duck-typed function call. 37 | ($.) : 38 | {t : Telescope a} 39 | -> (f : Obj sig) 40 | -> {auto pf : sig "__call__" = Call t} 41 | -> (args : a) 42 | -> PIO $ retTy t args 43 | ($.) {t = t} (MkObj f) args = 44 | unRaw <$> 45 | foreign FFI_Py "_idris_call" 46 | (Ptr -> List Dyn -> PIO (Raw $ Telescope.retTy t args)) 47 | f 48 | (strip t args) 49 | 50 | infixl 4 $: 51 | ||| Duck-typed function call, useful for chaining. 52 | ($:) : 53 | {t : Telescope a} 54 | -> (f : PIO (Obj sig)) 55 | -> {auto pf : sig "__call__" = Call t} 56 | -> (args : a) 57 | -> PIO $ retTy t args 58 | ($:) meth args = meth >>= \m => m $. args 59 | -------------------------------------------------------------------------------- /lib/Python/IO.idr: -------------------------------------------------------------------------------- 1 | module Python.IO 2 | 3 | import Python.Objects 4 | 5 | %default total 6 | %access public export 7 | 8 | unRaw : FFI_C.Raw a -> a 9 | unRaw (MkRaw x) = x 10 | 11 | ||| Supported Python foreign types. 12 | data PyTypes : Type -> Type where 13 | 14 | -- Primitive types 15 | PyInt_io : PyTypes Int 16 | PyNat_io : PyTypes Nat 17 | PyInteger_io : PyTypes Integer 18 | PyDouble_io : PyTypes Double 19 | PyBool_io : PyTypes Bool 20 | PyChar_io : PyTypes Char 21 | PyString_io : PyTypes String 22 | 23 | -- Other types 24 | PyUnit_io : PyTypes () 25 | PyPair_io : PyTypes a -> PyTypes b -> PyTypes (a, b) 26 | PyList_io : PyTypes a -> PyTypes (List a) 27 | PyFun_io : PyTypes a -> PyTypes b -> PyTypes (a -> b) 28 | PyMaybe_io : PyTypes a -> PyTypes (Maybe a) 29 | 30 | ||| Python objects, opaque to Idris. 31 | PyPtr_io : PyTypes Ptr 32 | 33 | ||| Arbitrary Idris objects, opaque to Python. 34 | PyAny_io : PyTypes (FFI_C.Raw a) 35 | 36 | ||| Python objects with a signature known to Idris. 37 | PyObj_io : PyTypes (Obj sig) 38 | 39 | FFI_Py : FFI 40 | FFI_Py = MkFFI PyTypes String String 41 | 42 | ||| Python IO. Read "pie-oh". 43 | PIO : Type -> Type 44 | PIO = IO' FFI_Py 45 | -------------------------------------------------------------------------------- /lib/Python/Lib/BeautifulSoup.idr: -------------------------------------------------------------------------------- 1 | module Python.Lib.BeautifulSoup 2 | 3 | import Python 4 | import Python.Prim 5 | 6 | %access public export 7 | %default total 8 | 9 | namespace Parsers 10 | 11 | LXML : String 12 | LXML = "lxml" 13 | 14 | HTML : String 15 | HTML = "html.parser" 16 | 17 | Element : Signature 18 | Element f = case f of 19 | "string" => Attr $ Maybe String 20 | "strings" => Attr . Obj $ PyList String 21 | _ => Object f 22 | 23 | Soup : Signature 24 | Soup f = case f of 25 | "select" => [String] ~~> Obj (PyList $ Obj Element) 26 | _ => Object f 27 | 28 | Bs4 : Signature 29 | Bs4 f = case f of 30 | "BeautifulSoup" => [String, String] ~~> Obj Soup 31 | _ => Module f 32 | 33 | import_ : PIO $ Obj Bs4 34 | import_ = importModule "bs4" 35 | -------------------------------------------------------------------------------- /lib/Python/Lib/Numpy.idr: -------------------------------------------------------------------------------- 1 | module Python.Lib.Numpy 2 | 3 | import Python 4 | import Python.Prim 5 | import Data.Erased 6 | 7 | %default total 8 | %access public export 9 | 10 | NDArray : Signature 11 | NDArray f = case f of 12 | "__str__" => [] ~~> String 13 | _ => Object f 14 | 15 | Arr : Type 16 | Arr = Obj NDArray 17 | 18 | ArithT : Type -> Signature 19 | ArithT a f = case f of 20 | "__add__" => [a, a] ~~> a 21 | "__mul__" => [a, a] ~~> a 22 | "__sub__" => [a, a] ~~> a 23 | "__div__" => [a, a] ~~> a 24 | "__str__" => [a] ~~> String 25 | _ => PyType f 26 | 27 | Mat : Nat -> Nat -> Maybe a -> Signature 28 | Mat _ _ _ = Object 29 | 30 | NDArrayT : Signature 31 | NDArrayT f = case f of 32 | "transpose" => [Arr] ~~> Arr 33 | _ => ArithT Arr f 34 | 35 | "transpose_fulldep" => fun $ 36 | forall r : Nat . 37 | forall c : Nat . 38 | forall a : Type . 39 | forall dtype : (Maybe a) . 40 | pi m : (Obj $ Mat r c dtype) . 41 | Return (Obj $ Mat c r dtype) 42 | 43 | Numpy : Signature 44 | Numpy f = case f of 45 | 46 | "array" => PAttr _ $ \a : Type => 47 | [Obj (PyList (Obj (PyList a))), String] ~> Arr 48 | 49 | "reshape" => [Arr, (Nat, Nat)] ~~> Arr 50 | "abs" => [Arr] ~~> Arr 51 | "dot" => [Arr, Arr] ~~> Arr 52 | "transpose" => [Arr] ~~> Arr 53 | "tile" => [Dyn, (Nat, Nat)] ~~> Arr 54 | "ndarray" => Attr $ Obj NDArrayT 55 | _ => Module f 56 | 57 | import_ : PIO $ Obj Numpy 58 | import_ = importModule "numpy" 59 | -------------------------------------------------------------------------------- /lib/Python/Lib/Numpy/Matrix.idr: -------------------------------------------------------------------------------- 1 | module Python.Lib.Numpy.Matrix 2 | 3 | import Python 4 | import Python.Prim 5 | import Python.Lib.Numpy 6 | 7 | import Data.Vect 8 | 9 | %access public export 10 | %default total 11 | 12 | -- private 13 | np : Obj Numpy 14 | np = unsafePerformIO Numpy.import_ 15 | 16 | -- private 17 | nda : Obj NDArrayT 18 | nda = np /. "ndarray" 19 | 20 | record DType (ty : Type) where 21 | constructor MkDType 22 | dtName : String 23 | dtFromInteger : Integer -> ty 24 | dtFromDouble : Double -> ty 25 | 26 | DDouble : DType Double 27 | DDouble = MkDType "float" fromInteger id 28 | 29 | -- abstract 30 | record Matrix (rows : Nat) (cols : Nat) (dtype : DType a) where 31 | constructor MkM 32 | arr : Obj NDArray 33 | 34 | -- private 35 | unsafeNp : PIO (Obj NDArray) -> Matrix r c dt 36 | unsafeNp = MkM . unsafePerformIO 37 | 38 | -- private 39 | op : (f : String) 40 | -> {auto pf : NDArrayT f = [Obj NDArray, Obj NDArray] ~~> Obj NDArray} 41 | -> Matrix r c dt -> Matrix r c dt -> Matrix r c dt 42 | op f (MkM x) (MkM y) = unsafeNp $ nda /. f $. [x, y] 43 | 44 | -- abstract 45 | fill : {dt : DType a} -> a -> Matrix r c dt 46 | fill {r=r} {c=c} x = unsafeNp $ np /. "tile" $. [toDyn x, (r,c)] 47 | 48 | fromInteger : Integer -> Matrix r c dt 49 | fromInteger {dt=dt} = fill . dtFromInteger dt 50 | 51 | fromDouble : Double -> Matrix r c dt 52 | fromDouble {dt=dt} = fill . dtFromDouble dt 53 | 54 | -- abstract 55 | singleton : {dt : DType a} -> a -> Matrix 1 1 dt 56 | singleton {a=a} {dt=dt} x = 57 | unsafeNp $ 58 | np //. FP "array" a $. [pyList [pyList [x]], dtName dt] 59 | 60 | -- abstract 61 | dot : Matrix r c dt -> Matrix c k dt -> Matrix r k dt 62 | dot (MkM x) (MkM y) = unsafeNp $ np /. "dot" $. [x,y] 63 | 64 | -- abstract 65 | transpose : Matrix r c dt -> Matrix c r dt 66 | transpose (MkM x) = unsafeNp $ np /. "transpose" $. [x] 67 | 68 | -- abstract 69 | array : (dt : DType a) -> Vect r (Vect c a) -> Matrix r c dt 70 | array {a=a} dt xs = unsafeNp $ np //. FP "array" a $. [c (map c xs), dtName dt] 71 | where 72 | c : {a : Type} -> Vect n a -> Obj (PyList a) 73 | c xs = pyList $ toList xs 74 | 75 | -- abstract 76 | reshape : Matrix r c dt -> {auto pf : r*c = r'*c'} -> Matrix r' c' dt 77 | reshape {r'=r'} {c'=c'} (MkM x) = 78 | unsafeNp $ 79 | np /. "reshape" $. [x, (r', c')] 80 | 81 | -- abstract 82 | (/) : Matrix r c dt -> Matrix r c dt -> Matrix r c dt 83 | (/) = op "__div__" 84 | 85 | -- abstract 86 | minus : Matrix r c dt -> Matrix r c dt -> Matrix r c dt 87 | minus = op "__sub__" 88 | 89 | -- abstract 90 | abs : Matrix r c dt -> Matrix r c dt 91 | abs (MkM x) = unsafeNp $ np /. "abs" $. [x] 92 | 93 | implementation Num (Matrix r c dt) where 94 | (+) = op "__add__" 95 | (*) = op "__mul__" 96 | fromInteger = Matrix.fromInteger 97 | 98 | implementation Show (Matrix r c dt) where 99 | show (MkM x) = unsafePerformIO $ x /. "__str__" $. [] 100 | -------------------------------------------------------------------------------- /lib/Python/Lib/Os.idr: -------------------------------------------------------------------------------- 1 | module Python.Lib.Os 2 | 3 | import Python 4 | 5 | %access public export 6 | %default total 7 | 8 | Os : Signature 9 | Os f = case f of 10 | "mkdir" => [String] ~~> Unit 11 | _ => Module f 12 | 13 | import_ : PIO (Obj Os) 14 | import_ = importModule "os" 15 | -------------------------------------------------------------------------------- /lib/Python/Lib/Queue.idr: -------------------------------------------------------------------------------- 1 | module Python.Lib.Queue 2 | 3 | import Python 4 | import Data.Erased 5 | 6 | %default total 7 | %access public export 8 | 9 | Queue : Type -> Signature 10 | Queue a f = case f of 11 | "put" => [a] ~~> () 12 | "get" => [Int] ~~> a 13 | "task_done" => [] ~~> () 14 | _ => Object f 15 | 16 | QueueM : Signature 17 | QueueM f = case f of 18 | "Queue" => fun $ 19 | forall a : Type . 20 | default maxSize : Int = 0 . -- default: 0 = no limit 21 | Return $ Obj (Queue a) 22 | 23 | _ => Module f 24 | 25 | import_ : PIO $ Obj QueueM 26 | import_ = importModule "Queue" -- this is lowercase in python3 27 | -------------------------------------------------------------------------------- /lib/Python/Lib/Requests.idr: -------------------------------------------------------------------------------- 1 | module Python.Lib.Requests 2 | 3 | import Python 4 | 5 | %access public export 6 | %default total 7 | 8 | Response : Signature 9 | Response f = case f of 10 | "text" => Attr String 11 | _ => Object f 12 | 13 | Session : Signature 14 | Session f = case f of 15 | "get" => [String] ~~> Obj Response 16 | _ => Object f 17 | 18 | Requests : Signature 19 | Requests f = case f of 20 | "Session" => [] ~~> Obj Session 21 | _ => Module f 22 | 23 | import_ : PIO $ Obj Requests 24 | import_ = importModule "requests" 25 | -------------------------------------------------------------------------------- /lib/Python/Lib/Threading.idr: -------------------------------------------------------------------------------- 1 | module Python.Lib.Threading 2 | 3 | import Python 4 | import Python.Prim 5 | import Python.Exceptions 6 | 7 | import Python.Lib.Queue 8 | 9 | %default total 10 | %access public export 11 | 12 | Thread : Signature 13 | Thread f = case f of 14 | "start" => [] ~~> () 15 | "join" => [] ~~> () 16 | _ => Object f 17 | 18 | Threading : Signature 19 | Threading f = case f of 20 | -- the first arg must always be Nothing, according to Python spec 21 | "Thread" => [Maybe Void, [] ~> ()] ~~> Obj Thread 22 | _ => Module f 23 | 24 | import_ : PIO $ Obj Threading 25 | import_ = importModule "threading" 26 | 27 | ||| Fork a side thread. The thread will send its result 28 | ||| through the queue that is returned by this function. 29 | forkPIO : PIO a -> PIO (Obj $ Queue a) 30 | forkPIO {a = a} work = do 31 | queue <- Queue.import_ /: "Queue" $: [Erase a, Just 1] 32 | thread <- Threading.import_ /: "Thread" $: [Nothing, marshalPIO $ worker queue] 33 | thread /. "start" $. [] 34 | 35 | pure queue 36 | where 37 | worker : (Obj $ Queue a) -> PIO () 38 | worker queue = do 39 | result <- work 40 | queue /. "put" $. [result] 41 | 42 | ||| Wait for the result of a side thread. 43 | wait : Obj (Queue a) -> PIO a 44 | wait q = q /. "get" $. [1] 45 | -------------------------------------------------------------------------------- /lib/Python/Objects.idr: -------------------------------------------------------------------------------- 1 | module Python.Objects 2 | 3 | import Python.Telescope 4 | 5 | %default total 6 | %access public export 7 | 8 | data Field : Type where 9 | PAttr : (pt : Type) -> (tf : pt -> Type) -> Field 10 | Call : (t : Telescope a) -> Field 11 | NotField : Field 12 | 13 | Attr : Type -> Field 14 | Attr t = PAttr Unit $ const t 15 | 16 | ||| Python object signature is a list of its fields, plus mixins. 17 | Signature : Type 18 | Signature = String -> Field 19 | 20 | implementation Semigroup Signature where 21 | (<+>) s t = 22 | \field => case s field of 23 | NotField => t field 24 | result => result 25 | 26 | implementation Monoid Signature where 27 | neutral = const NotField 28 | 29 | ||| Dynamically typed Python reference. 30 | Dyn : Type 31 | Dyn = Ptr 32 | 33 | export 34 | toDyn : a -> Dyn 35 | toDyn = believe_me 36 | 37 | ||| Python object, typed by its signature. 38 | -- TODO: make this abstract 39 | record Obj (sig : Signature) where 40 | constructor MkObj 41 | ptr : Ptr 42 | 43 | ||| Python type of Python types. 44 | PyType : Signature 45 | PyType f = case f of 46 | "__name__" => Attr String 47 | _ => NotField 48 | 49 | ||| Object that all Python objects inherit from. 50 | Object : Signature 51 | Object f = case f of 52 | "__class__" => Attr $ Obj PyType 53 | _ => NotField 54 | 55 | ||| Python modules. 56 | Module : Signature 57 | Module f = case f of 58 | "__name__" => Attr String 59 | _ => Object f 60 | -------------------------------------------------------------------------------- /lib/Python/Prim.idr: -------------------------------------------------------------------------------- 1 | module Python.Prim 2 | 3 | import Python 4 | import Python.Telescope 5 | import Python.Fields 6 | import Python.Functions 7 | 8 | %access public export 9 | %default total 10 | 11 | ||| The actual state of iteration. 12 | Iterator : Type -> Signature 13 | Iterator a f = case f of 14 | "next" => [] ~~> a 15 | _ => Object f 16 | 17 | ||| Something that can produce an iterator. 18 | Iterable : Type -> Signature 19 | Iterable a f = case f of 20 | "__iter__" => [] ~~> Obj (Iterator a) 21 | _ => Object f 22 | 23 | ||| Python string as object. 24 | PyString : Signature 25 | PyString f = case f of 26 | "join" => [Obj $ Iterable String] ~~> String 27 | _ => Iterable Char f 28 | 29 | ||| Python list as object. 30 | PyList : Type -> Signature 31 | PyList a = Iterable a -- nothing else yet 32 | 33 | ||| Primitives promotable to objects. 34 | data PythonPrim : Type -> Signature -> Type where 35 | PPString : PythonPrim String PyString 36 | 37 | namespace Builtins 38 | Builtins : Signature 39 | Builtins f = case f of 40 | "list" => fun $ 41 | forall a : Type . 42 | pi xs : (List a) . 43 | Return $ Obj (PyList a) 44 | 45 | _ => Module f 46 | 47 | builtins : Obj Builtins 48 | builtins = getGlobal "__builtins__" 49 | 50 | pyList : List a -> Obj $ PyList a 51 | pyList {a=a} xs = unsafePerformIO (builtins /. "list" $. [Erase a, xs]) 52 | 53 | ||| Promote a primitive to an object. Note that this is a no-oop, 54 | ||| all primitives already are objects in Python. 55 | obj : (x : a) -> {auto pf : PythonPrim a sig} -> Obj sig 56 | obj x = believe_me x 57 | 58 | ||| Get the next value from an iterator. 59 | next : Obj (Iterator a) -> PIO (Maybe a) 60 | next {a = a} it = do 61 | OK x <- try (it /. "next" $. []) 62 | | Except StopIteration e => pure Nothing 63 | | Except _ e => raise e 64 | pure $ Just x 65 | 66 | ||| A left-fold over an iterable object, implemented in Idris. 67 | ||| This is not very efficient (TCO does not kick in here) 68 | ||| but it's a demonstration we can do it easily. 69 | ||| 70 | ||| See also `foreach`, which is a native Python for-loop. 71 | partial 72 | iterate : (o : Obj sig) 73 | -> (st : b) 74 | -> (f : b -> a -> PIO b) 75 | -> {auto pf : sig "__iter__" = [] ~~> Obj (Iterator a)} 76 | -> PIO b 77 | iterate iterable st f = do 78 | iterator <- iterable /. "__iter__" $. [] 79 | iter iterator st f 80 | where 81 | partial 82 | iter : Obj (Iterator a) -> (st : b) -> (f : b -> a -> PIO b) -> PIO b 83 | iter it st f = do 84 | Just x <- next it | Nothing => pure st 85 | st' <- f st x 86 | iter it st' f 87 | 88 | ||| A left-fold over an iterable object, implemented as a for-loop in Python. 89 | ||| 90 | ||| @ o The iterable object. 91 | ||| @ st Initial state. 92 | ||| @ f PIO action called for every element, transforms the state. 93 | partial 94 | foreach : 95 | (o : Obj sig) 96 | -> (st : b) 97 | -> (f : b -> a -> PIO b) 98 | -> {auto pf : sig "__iter__" = [] ~~> Obj (Iterator a)} 99 | -> PIO b 100 | foreach {a=a} {b=b} {sig=sig} iterable st f = do 101 | iterator <- iterable /. "__iter__" $. [] 102 | unRaw <$> 103 | foreign FFI_Py "_idris_foreach" 104 | (Obj sig -> Raw b -> Raw (b -> a -> PIO b) -> PIO (Raw b)) 105 | iterable 106 | (MkRaw st) 107 | (MkRaw f) 108 | 109 | ||| Collect all elements of an iterator into a list. 110 | partial 111 | collect : (it : Obj sig) -> {auto pf : sig "__iter__" = [] ~~> Obj (Iterator a)} -> PIO (List a) 112 | collect it = reverse <$> foreach it List.Nil (\xs, x => pure (x :: xs)) 113 | -------------------------------------------------------------------------------- /lib/Python/Telescope.idr: -------------------------------------------------------------------------------- 1 | module Python.Telescope 2 | 3 | import public Data.Erased 4 | 5 | %default total 6 | %access public export 7 | 8 | data Binder : (argTy : Type) -> (depTy : Type) -> (argTy -> depTy) -> Type where 9 | ||| Relevant, mandatory, positional argument. 10 | Pi : (a : Type) -> Binder a a Basics.id 11 | 12 | ||| Erased, mandatory, positional argument. 13 | Forall : (a : Type) -> Binder (Erased a) (Erased a) Basics.id 14 | 15 | ||| An argument with a default value. 16 | Default : (a : Type) -> (d : a) -> Binder (Maybe a) a (fromMaybe d) 17 | 18 | ||| Type of sequences where the value of any element may affect 19 | ||| the type of the following elements. 20 | ||| 21 | ||| In other words, a dependent pair generalised to multiple elements. 22 | data Telescope : Type -> Type where 23 | 24 | ||| Empty telescope. 25 | Return : Type -> Telescope Unit 26 | 27 | ||| Value on which subsequent types may depend. 28 | Bind : 29 | (bnd : Binder argTy depTy fromArg) 30 | -> {b : depTy -> Type} 31 | -> (tf : (x : depTy) -> Telescope (b x)) 32 | -> Telescope (DPair argTy (b . fromArg)) 33 | 34 | term syntax "pi" {x} ":" [t] "." [rhs] 35 | = Bind (Pi t) (\x : t => rhs); 36 | 37 | term syntax "forall" {x} ":" [t] "." [rhs] 38 | = Bind (Forall t) (\ex : Erased t => let x = unerase ex in rhs); 39 | 40 | term syntax "default" {x} ":" [t] "=" [dflt] "." [rhs] 41 | = Bind (Default t dflt) (\x : t => rhs); 42 | 43 | namespace DPairSugar 44 | Nil : Type 45 | Nil = Unit 46 | 47 | (::) : Type -> Type -> Type 48 | (::) a b = DPair a (const b) 49 | 50 | namespace TupleSugar 51 | ||| Alternative name for `MkUnit`, useful for the [list, syntax, sugar]. 52 | Nil : Unit 53 | Nil = () 54 | 55 | ||| Infix name for `MkDPair`, useful for the [list, syntax, sugar]. 56 | (::) : (x : a) -> (y : b x) -> DPair a b 57 | (::) = MkDPair 58 | 59 | ||| Convert a list of types to the corresponding tuple type. 60 | toTuple : (xs : List Type) -> Type 61 | toTuple [] = Unit 62 | toTuple (x :: xs) = DPair x (const $ toTuple xs) 63 | 64 | ||| Convert a list of types to the corresponding simple telescope. 65 | simple : (xs : List Type) -> (ret : Type) -> Telescope (toTuple xs) 66 | simple [] ret = Return ret 67 | simple (a :: as) ret = Bind (Pi a) (\x => simple as ret) 68 | 69 | retTy : (t : Telescope a) -> (args : a) -> Type 70 | retTy (Return x) () = x 71 | retTy (Bind {fromArg = fromArg} bnd tf) (MkDPair x xs) = retTy (tf $ fromArg x) xs 72 | -------------------------------------------------------------------------------- /lib/python.ipkg: -------------------------------------------------------------------------------- 1 | package python 2 | 3 | modules = Python 4 | , Python.Prim 5 | , Python.IO 6 | , Python.Telescope 7 | , Python.Objects 8 | , Python.Exceptions 9 | , Python.Functions 10 | , Python.Fields 11 | , Python.Lib.Os 12 | , Python.Lib.Queue 13 | , Python.Lib.Threading 14 | , Python.Lib.Requests 15 | , Python.Lib.BeautifulSoup 16 | , Python.Lib.Numpy 17 | , Python.Lib.Numpy.Matrix 18 | -------------------------------------------------------------------------------- /run-tests.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | die() { 4 | echo "!! $1, aborting" >&2 5 | exit 1 6 | } 7 | 8 | make-deterministic() { 9 | prog="$1" 10 | 11 | case "$prog" in 12 | example) 13 | sed -r 's/thread [AB] done/thread A or B done/' 14 | ;; 15 | 16 | *) cat # no changes 17 | ;; 18 | esac 19 | } 20 | 21 | run() { 22 | prog="$1" 23 | temp="$(tempfile)" 24 | 25 | echo "$1" 26 | 27 | echo " compiling..." 28 | idris -p python --codegen python examples/"$1".idr -o examples/"$1".py \ 29 | || die "could not compile $1" 30 | 31 | expected=examples/"$1".expected.txt 32 | echo " running..." 33 | if [ "$update_expected" = 1 ]; then 34 | { python examples/"$1".py \ 35 | || die "could not run $1" 36 | } | make-deterministic "$1" \ 37 | > "$expected" 38 | else 39 | { python examples/"$1".py \ 40 | || die "could not run $1" 41 | } | make-deterministic "$1" \ 42 | > "$temp" 43 | 44 | diff "$temp" "$expected" \ 45 | || die "unexpected output (see: vimdiff $temp $expected)" 46 | 47 | rm "$temp" 48 | fi 49 | } 50 | 51 | if [ "$1" = "update-expected" ]; then 52 | update_expected=1 53 | else 54 | update_expected=0 55 | fi 56 | 57 | (cd examples; ls -1 *.idr) | while read fname; do 58 | run "${fname%.idr}" 59 | done 60 | -------------------------------------------------------------------------------- /src/IRTS/CodegenPython.hs: -------------------------------------------------------------------------------- 1 | {-# LANGUAGE PatternGuards #-} 2 | module IRTS.CodegenPython (codegenPython) where 3 | 4 | import IRTS.CodegenCommon 5 | import IRTS.Lang hiding (lift) 6 | import IRTS.Simplified 7 | import IRTS.Defunctionalise hiding (lift) 8 | 9 | import Idris.Core.TT 10 | 11 | import Numeric 12 | import Data.Maybe 13 | import Data.Char 14 | import Data.List 15 | import Data.Ord 16 | import qualified Data.Text as T 17 | import qualified Data.Map as M 18 | 19 | import Control.Applicative hiding (empty, Const) 20 | import Control.Monad 21 | import Control.Monad.Trans.State.Lazy 22 | import Control.Monad.Trans.Reader 23 | import Control.Monad.Trans.Class 24 | 25 | import Util.PrettyPrint 26 | 27 | -- Codegen state within one Decl 28 | data CGState = CGState 29 | { varCounter :: Int -- for fresh variables 30 | } 31 | deriving (Show) 32 | 33 | data CGCtx = CGCtx 34 | { ctorTags :: M.Map Name Int -- constructor tags 35 | , curFun :: (Name, [Name]) -- name, args for tail calls 36 | } 37 | deriving (Show) 38 | 39 | -- Make the distinction for the humans reading the code 40 | type Stmts = Doc -- statements 41 | type Expr = Doc -- expressions 42 | 43 | -- A code generator for "a" generates: 44 | -- 45 | -- 1. statements that prepare context for "a" 46 | -- 2. expression that stands for "a" afterwards 47 | -- 48 | -- Note that (1.) is not part of the state; 49 | -- it's a part of the what the given DExp translates to. 50 | -- 51 | -- (2.) is always an expression in Python 52 | -- and therefore composes into bigger expressions easily, 53 | -- while (1.) are statements and they can only be sequenced. 54 | -- 55 | -- The idea is that, for example, f(x, y) compiles to: 56 | -- x_statements 57 | -- y_statements 58 | -- f(x_expr, y_expr) 59 | -- 60 | newtype CG a = CG { runCG :: ReaderT CGCtx (State CGState) (Stmts, a) } 61 | 62 | -- Now let's say how Functor/Applicative/Monad deal 63 | -- with the division into (Stmt, Expr) 64 | instance Functor CG where 65 | fmap f (CG x) = CG $ do 66 | (stmts, expr) <- x 67 | return (stmts, f expr) 68 | 69 | instance Applicative CG where 70 | pure x = CG (return (empty, x)) 71 | CG f <*> CG x = CG $ do 72 | (stf, f') <- f 73 | (stx, x') <- x 74 | return (stf $+$ stx, f' x') 75 | 76 | instance Monad CG where 77 | return = pure 78 | CG x >>= f = CG $ do 79 | (stx, x') <- x 80 | (sty, y') <- runCG $ f x' 81 | return (stx $+$ sty, y') 82 | 83 | smap :: (Stmts -> Stmts) -> CG a -> CG a 84 | smap f (CG x) = CG $ do 85 | (stmts, expr) <- x 86 | return (f stmts, expr) 87 | 88 | emit :: Stmts -> CG () 89 | emit stmts = CG $ return (stmts, ()) 90 | 91 | sindent :: CG a -> CG a 92 | sindent = smap indent 93 | 94 | fresh :: CG LVar 95 | fresh = CG $ do 96 | CGState vc <- lift get 97 | lift . put $ CGState (vc + 1) 98 | return (empty, Loc (-vc)) 99 | 100 | ctorTag :: Name -> CG (Maybe Int) 101 | ctorTag n = CG $ do 102 | ctors <- asks ctorTags 103 | return (empty, M.lookup n ctors) 104 | 105 | currentFn :: CG (Name, [Name]) 106 | currentFn = CG $ do 107 | cf <- asks curFun 108 | return (empty, cf) 109 | 110 | indent :: Doc -> Doc 111 | indent = nest 2 112 | 113 | pythonPreamble :: Doc 114 | pythonPreamble = vcat . map text $ 115 | [ "#!/usr/bin/env python" 116 | , "" 117 | , "import sys" 118 | , "import importlib" 119 | , "import math" 120 | , "" 121 | , "Unit = object()" 122 | , "World = object()" 123 | , "" 124 | , "class IdrisError(Exception):" 125 | , " pass" 126 | , "" 127 | , "def _idris_error(msg):" 128 | , " raise IdrisError(msg)" 129 | , "" 130 | , "def _idris_pymodule(name):" 131 | , " return importlib.import_module(name)" 132 | , "" 133 | , "def _idris_call(f, args):" 134 | , " return f(*list(args))" 135 | , "" 136 | , "def _idris_foreach(it, st, f):" 137 | , " for x in it:" 138 | , " # Apply st, x, world" 139 | , " st = APPLY0(APPLY0(APPLY0(f, st), x), World)" 140 | , " return st" 141 | , "" 142 | , "def _idris_try(f, fail, succ):" 143 | , " try:" 144 | , " result = APPLY0(f, World) # apply to world" 145 | , " return APPLY0(succ, result)" 146 | , " except Exception as e:" 147 | , " return APPLY0(fail, e)" 148 | , "" 149 | , "def _idris_raise(e):" 150 | , " raise e" 151 | , "" 152 | , "def _idris_marshal_PIO(action):" 153 | , " return lambda: APPLY0(action, World) # delayed apply-to-world" 154 | , "" 155 | , "def _idris_get_global(name):" 156 | , " return globals()[name]" 157 | , "" 158 | , "class _ConsIter(object):" 159 | , " def __init__(self, node):" 160 | , " self.node = node" 161 | , "" 162 | , " def next(self):" 163 | , " if self.node.isNil:" 164 | , " raise StopIteration" 165 | , " else:" 166 | , " result = self.node.head" 167 | , " self.node = self.node.tail" 168 | , " return result" 169 | , "" 170 | , "class ConsList(object):" 171 | , " def __init__(self, isNil=True, head=None, tail=None):" 172 | , " self.isNil = isNil" 173 | , " self.head = head" 174 | , " self.tail = tail" 175 | , "" 176 | , " def __nonzero__(self):" 177 | , " return not self.isNil" 178 | , "" 179 | , " def __len__(self):" 180 | , " cnt = 0" 181 | , " while not self.isNil:" 182 | , " self = self.tail" 183 | , " cnt += 1" 184 | , " return cnt" 185 | , "" 186 | , " def cons(self, x):" 187 | , " return ConsList(isNil=False, head=x, tail=self)" 188 | , "" 189 | , " def __iter__(self):" 190 | , " return _ConsIter(self)" 191 | , "" 192 | ] 193 | 194 | pythonLauncher :: Doc 195 | pythonLauncher = 196 | text "if" <+> text "__name__" <+> text "==" <+> text "'__main__'" <> colon 197 | $+$ indent ( 198 | cgApp (cgName $ sMN 0 "runMain") [] 199 | ) 200 | 201 | -- The prefix "_" makes all names "hidden". 202 | -- This is useful when you import the generated module from Python code. 203 | mangle :: Name -> String 204 | mangle n = "_idris_" ++ concatMap mangleChar (showCG n) 205 | where 206 | mangleChar x 207 | | isAlpha x || isDigit x = [x] 208 | | otherwise = "_" ++ show (ord x) ++ "_" 209 | 210 | -- We could generate from: 211 | -- simpleDecls / defunDecls / liftDecls 212 | codegenPython :: CodeGenerator 213 | codegenPython ci = writeFile (outputFile ci) (render "#" source) 214 | where 215 | source = pythonPreamble $+$ definitions $+$ exports $+$ pythonLauncher 216 | 217 | -- main file 218 | decls = defunDecls ci 219 | ctors = M.fromList [(n, tag) | (n, DConstructor n' tag arity) <- decls] 220 | definitions = vcat $ map (cgDef ctors) [d | d@(_, DFun _ _ _) <- decls] 221 | 222 | -- all exports 223 | exports = vcat $ concatMap cgExport (exportDecls ci) 224 | 225 | cgExport :: ExportIFace -> [Doc] 226 | cgExport (Export _ffiName _fileName es) = map cgExportDecl es 227 | 228 | cgExportDecl :: Export -> Doc 229 | cgExportDecl (ExportFun fn (FStr en) (FIO ret) argTys) 230 | = cgExportFun fn en (length argTys) 231 | cgExportDecl _ = empty -- ignore everything else 232 | -- Example: ExportFun Main.exports, greet (FStr "greet") (FIO (FCon PyUnit)) [] 233 | 234 | cgExportFun :: Name -> String -> Int -> Doc 235 | cgExportFun fn en argCnt 236 | = (empty "export: " ++ show fn) 237 | $+$ text "def" <+> cgApp (text en) (map text args) <> colon 238 | $+$ indent ( 239 | cgApp 240 | (cgName (sMN 0 "APPLY")) 241 | [ cgApp (cgName fn) 242 | $ map text args 243 | , text "World" 244 | ] 245 | ) 246 | $+$ text "" 247 | where 248 | args = ["arg" ++ show i | i <- [1..argCnt]] 249 | 250 | -- Let's not mangle /that/ much. Especially function parameters 251 | -- like e0 and e1 are nicer when readable. 252 | cgName :: Name -> Expr 253 | cgName (MN i n) | all (\x -> isAlpha x || x `elem` "_") (T.unpack n) 254 | = text $ T.unpack n ++ show i 255 | cgName n = text (mangle n) -- show n -- uncomment this to get a comment for *every* mangled name 256 | 257 | bigParens :: Doc -> Doc 258 | bigParens d = lparen $+$ indent d $+$ rparen 259 | 260 | bigBraces :: Doc -> Doc 261 | bigBraces d = lbrace $+$ indent d $+$ rbrace 262 | 263 | cgTuple :: Int -> [Expr] -> Expr 264 | cgTuple maxSize [] = parens empty -- don't split empty tuples 265 | cgTuple maxSize xs 266 | | size oneLiner <= maxSize = oneLiner 267 | | allSmall = bigParens $ vsepLines lines -- for arg lists where every item is just a token 268 | | otherwise = bigParens $ vsepLines xs 269 | where 270 | oneLiner = parens (hsep $ punctuate comma xs) 271 | vsepLines = vcat . punctuate comma 272 | allSmall = and [size x < 8 | x <- xs] 273 | lines = wrapLines 60 empty xs 274 | 275 | wrapLines :: Int -> Doc -> [Doc] -> [Doc] 276 | wrapLines w curLine [] 277 | | size curLine == 0 = [] 278 | | otherwise = [curLine] 279 | wrapLines w curLine (x : xs) 280 | | curSize >= w = curLine : wrapLines w x xs 281 | | curSize == 0 = wrapLines w x xs 282 | | otherwise = wrapLines w (curLine <> comma <+> x) xs 283 | where 284 | curSize = size curLine 285 | 286 | cgApp :: Expr -> [Expr] -> Expr 287 | cgApp f args = f <> cgTuple maxWidth args 288 | where 289 | maxWidth = 80 - width f 290 | 291 | -- Process one definition. The caller deals with constructor declarations, 292 | -- we only deal with function definitions. 293 | cgDef :: M.Map Name Int -> (Name, DDecl) -> Doc 294 | cgDef ctors (n, DFun name' args body) = 295 | (empty show name') 296 | $+$ (text "def" <+> cgApp (cgName n) (map cgName args) <> colon) 297 | $+$ indent ( 298 | text "while" <+> text "True" <> colon -- for tail calls 299 | $+$ indent ( 300 | -- trace $+$ -- uncomment this line to enable printing traces 301 | statements 302 | $+$ text "return" <+> retVal 303 | ) 304 | ) 305 | $+$ text "" -- empty line separating definitions 306 | where 307 | (statements, retVal) = evalState (runReaderT body' initCtx) initState 308 | body' = runCG . cgExp True $ body 309 | initCtx = CGCtx ctors (n, args) 310 | initState = CGState 1 311 | 312 | -- used only for debugging 313 | trace = text "print" <+> text (show $ mangle n ++ "(" ++ argfmt ++ ")") 314 | <+> text "%" <+> cgTuple 80 [text "repr" <> parens (cgName a) | a <- args] 315 | argfmt = intercalate ", " ["%s" | _ <- args] 316 | 317 | cgVar :: LVar -> Expr 318 | cgVar (Loc i) 319 | | i >= 0 = text "loc" <> int i 320 | | otherwise = text "aux" <> int (-i) 321 | cgVar (Glob n) = cgName n 322 | 323 | cgError :: String -> Expr 324 | cgError msg = text "_idris_error" <> parens (text $ show msg) 325 | 326 | cgExtern :: String -> [Expr] -> Expr 327 | cgExtern "prim__null" args = text "None" 328 | cgExtern n args = cgError $ "unimplemented external: " ++ n 329 | 330 | -- Notation for python bracketed[indexing]. 331 | (!) :: Expr -> String -> Expr 332 | x ! i = x <> brackets (text i) 333 | 334 | cgPOp :: String -> [Expr] -> Expr 335 | cgPOp op [x, y] = parens $ x <+> text op <+> y 336 | 337 | cgPFun :: String -> [Expr] -> Expr 338 | cgPFun fun = cgApp $ text fun 339 | 340 | cgPrim :: PrimFn -> [Expr] -> Expr 341 | cgPrim (LPlus _) = cgPOp "+" 342 | cgPrim (LMinus _) = cgPOp "-" 343 | cgPrim (LTimes _) = cgPOp "*" 344 | cgPrim (LUDiv _) = cgPOp "/" 345 | cgPrim (LSDiv _) = cgPOp "/" 346 | cgPrim (LURem _) = cgPOp "%" 347 | cgPrim (LSRem _) = cgPOp "%" 348 | 349 | cgPrim (LAnd _) = cgPOp "&" 350 | cgPrim (LOr _) = cgPOp "|" 351 | cgPrim (LXOr _) = cgPOp "^" 352 | cgPrim (LSHL _) = cgPOp "<<" 353 | cgPrim (LASHR _) = cgPOp ">>" 354 | cgPrim (LLSHR _) = cgPOp ">>" -- because Python numbers have an infinite number of bits, LSHR and ASHR coincide 355 | cgPrim (LCompl _) = \[x] -> text "~" <> x 356 | 357 | cgPrim (LEq _) = cgPOp "==" 358 | cgPrim (LLt _) = cgPOp "<" 359 | cgPrim (LSLt _) = cgPOp "<" 360 | cgPrim (LLe _) = cgPOp "<=" 361 | cgPrim (LSLe _) = cgPOp "<=" 362 | cgPrim (LGt _) = cgPOp ">" 363 | cgPrim (LSGt _) = cgPOp ">" 364 | cgPrim (LGe _) = cgPOp ">=" 365 | cgPrim (LSGe _) = cgPOp ">=" 366 | 367 | -- this is probably not entirely right 368 | cgPrim (LSExt _ _) = head 369 | cgPrim (LZExt _ _) = head 370 | cgPrim (LTrunc _ _) = head 371 | cgPrim (LBitCast _ _) = head 372 | 373 | cgPrim (LIntStr _) = cgPFun "str" 374 | cgPrim (LStrInt _) = cgPFun "int" 375 | cgPrim LStrRev = \[x] -> x ! "::-1" 376 | cgPrim LStrConcat = cgPOp "+" 377 | cgPrim LStrCons = cgPOp "+" 378 | cgPrim LStrLt = cgPOp "<" 379 | cgPrim LStrEq = cgPOp "==" 380 | cgPrim LStrHead = \[x] -> x ! "0" 381 | cgPrim LStrTail = \[x] -> x ! "1:" 382 | cgPrim LStrIndex = \[x,i] -> x <> brackets i 383 | cgPrim LStrLen = cgPFun "len" 384 | 385 | cgPrim LStrSubstr = \[ofs,len,s] -> s <> brackets (ofs <> colon <> cgPOp "+" [ofs,len]) 386 | 387 | cgPrim (LChInt _) = cgPFun "ord" 388 | cgPrim (LIntCh _) = cgPFun "unichr" 389 | 390 | cgPrim LWriteStr = \[world, s] -> text "sys.stdout.write" <> parens s 391 | cgPrim LReadStr = \_ -> text "sys.stdin.readline()" 392 | 393 | cgPrim (LExternal n) = cgExtern $ show n 394 | cgPrim (LIntFloat _) = cgPFun "float" 395 | cgPrim (LFloatInt _) = cgPFun "int" 396 | cgPrim LFloatStr = cgPFun "str" 397 | cgPrim LStrFloat = cgPFun "float" 398 | 399 | cgPrim LFExp = cgPFun "math.exp" 400 | cgPrim LFLog = cgPFun "math.log" 401 | cgPrim LFSin = cgPFun "math.sin" 402 | cgPrim LFCos = cgPFun "math.cos" 403 | cgPrim LFTan = cgPFun "math.tan" 404 | cgPrim LFASin = cgPFun "math.asin" 405 | cgPrim LFACos = cgPFun "math.acos" 406 | cgPrim LFATan = cgPFun "math.atan" 407 | cgPrim LFSqrt = cgPFun "math.sqrt" 408 | cgPrim LFFloor = cgPFun "math.floor" 409 | cgPrim LFCeil = cgPFun "math.ceil" 410 | cgPrim LFNegate = \[x] -> text "-" <> x 411 | 412 | cgPrim f = \args -> cgError $ "unimplemented prim: " ++ show f ++ ", args = " ++ show args 413 | 414 | cgConst :: Const -> Expr 415 | cgConst (I i) = text $ show i 416 | cgConst (BI i) = text $ show i 417 | cgConst (Fl f) = text $ show f 418 | cgConst (Ch c) = text $ pyShowStr [c] 419 | cgConst (Str s) = text $ pyShowStr s 420 | cgConst c = cgError $ "unimplemented constant: " ++ show c 421 | 422 | pyShowStr :: String -> String 423 | pyShowStr s = "u'" ++ concatMap pyShowChr s ++ "'" 424 | 425 | pyShowChr :: Char -> String 426 | pyShowChr '\'' = "\\'" 427 | pyShowChr '\\' = "\\\\" 428 | pyShowChr c 429 | | c >= ' ' && c < '\x7F' = [c] 430 | | c <= '\xFFFF' = "\\u" ++ showHexN 4 (ord c) 431 | | otherwise = "\\U" ++ showHexN 8 (ord c) 432 | 433 | showHexN :: Int -> Int -> String 434 | showHexN 0 _ = "" 435 | showHexN w n = 436 | let (p,q) = n `divMod` 16 437 | in showHexN (w-1) p ++ showHex q "" 438 | 439 | cgCtor :: Int -> Name -> [Expr] -> Expr 440 | cgCtor tag n [] = parens (int tag <> comma) show n 441 | cgCtor tag n args = cgTuple 80 $ (int tag show n) : args 442 | 443 | cgAssign :: LVar -> Expr -> Stmts 444 | cgAssign v e = cgVar v <+> text "=" <+> e 445 | 446 | cgAssignN :: Name -> Expr -> Stmts 447 | cgAssignN n e = cgName n <+> text "=" <+> e 448 | 449 | cgAssignMany :: [Name] -> [Expr] -> Stmts 450 | cgAssignMany ns es = 451 | hsep [cgName n <> comma | n <- ns] 452 | <+> text "=" 453 | <+> hsep [e <> comma | e <- es] 454 | 455 | -- pattern-matching / tuple decomposition 456 | cgMatch :: [LVar] -> LVar -> Stmts 457 | cgMatch [] rhs = empty 458 | cgMatch [v] rhs = cgVar v <+> text "=" <+> cgVar rhs <> text "[1]" 459 | cgMatch lhs rhs = 460 | hsep (punctuate comma $ map cgVar lhs) 461 | <+> text "=" 462 | <+> cgVar rhs <> text "[1:]" 463 | 464 | cgTailCall :: [Name] -> [Expr] -> CG Expr 465 | cgTailCall argNames args = do 466 | emit $ cgAssignMany argNames args 467 | emit $ text "continue" 468 | return $ cgError "unreachable due to tail call" 469 | 470 | cgExp :: Bool -> DExp -> CG Expr 471 | cgExp tailPos (DV var) = return $ cgVar (Glob var) 472 | cgExp tailPos (DApp tc n args) = do 473 | tag <- ctorTag n 474 | case tag of 475 | Just t -> cgExp True (DC Nothing t n args) -- application of ctor 476 | Nothing -> do 477 | (curFn, argNames) <- currentFn 478 | if tailPos && n == curFn 479 | then cgTailCall argNames =<< mapM (cgExp False) args -- tail call! 480 | else cgApp (cgName n) <$> mapM (cgExp False) args -- ordinary call 481 | 482 | cgExp tailPos (DLet n v e) = do 483 | emit . cgAssignN n =<< cgExp False v 484 | cgExp tailPos e 485 | 486 | cgExp tailPos (DUpdate n e) = return . cgError $ "unimplemented SUpdate for " ++ show n ++ " and " ++ show e 487 | 488 | cgExp tailPos (DC _ tag n args) 489 | | Just (ctor, test, match) <- specialCased n = ctor <$> mapM (cgExp False) args 490 | | otherwise = cgCtor tag n <$> mapM (cgExp False) args 491 | 492 | -- if the scrutinee is something big, save it into a variable 493 | -- because we'll copy it into a possibly long chain of if-elif-... 494 | cgExp tailPos (DCase caseType (DV var) alts) = cgCase tailPos (Glob var) alts 495 | cgExp tailPos (DCase caseType e alts) = do 496 | scrutinee <- fresh 497 | emit . cgAssign scrutinee =<< cgExp False e 498 | cgCase tailPos scrutinee alts 499 | 500 | cgExp tailPos (DChkCase (DV var) alts) = cgCase tailPos (Glob var) alts 501 | cgExp tailPos (DChkCase e alts) = do 502 | scrutinee <- fresh 503 | emit . cgAssign scrutinee =<< cgExp False e 504 | cgCase tailPos scrutinee alts 505 | 506 | cgExp tailPos (DProj e i) = do 507 | e <- cgExp False e 508 | return $ e ! show (i+1) 509 | 510 | cgExp tailPos (DConst c) = return $ cgConst c 511 | 512 | cgExp tailPos (DForeign fdesc (FStr fn) args) = cgApp (text fn) <$> mapM (cgExp False . snd) args 513 | cgExp tailPos (DForeign fdesc rdesc args) = error $ "unrecognised foreign: " ++ show (fdesc, rdesc, args) 514 | cgExp tailPos (DOp prim args) = cgPrim prim <$> mapM (cgExp False) args 515 | cgExp tailPos DNothing = return $ text "None" 516 | cgExp tailPos (DError msg) = return $ cgError msg 517 | 518 | data IfElif = If | Elif | Else | Assert 519 | 520 | zipIfElif :: [a] -> [(IfElif, a)] 521 | zipIfElif [] = [] 522 | zipIfElif [x] = [(Assert, x)] 523 | zipIfElif (x : xs) = (If, x) : elif xs 524 | where 525 | elif [x] = [(Else, x)] 526 | elif (x : xs) = (Elif, x) : elif xs 527 | elif [] = error "elif: can't happen" 528 | 529 | -- We assume that all tags are different here 530 | cgAltTree :: Int -> Int -> Maybe LVar -> LVar -> [(Int, DAlt)] -> CG () 531 | cgAltTree groupSize altCount retVar scrutinee alts 532 | | altCount > groupSize 533 | = do 534 | emit $ text "if" <+> cgVar scrutinee <> text "[0] <" <+> int firstHi <> colon 535 | sindent $ cgAltTree groupSize lo retVar scrutinee (take lo alts) 536 | emit $ text "else" <> colon 537 | sindent $ cgAltTree groupSize (altCount - lo) retVar scrutinee (drop lo alts) 538 | where 539 | lo = altCount `div` 2 540 | firstHi = fst (alts !! lo) 541 | 542 | cgAltTree groupSize altCount retVar scrutinee alts 543 | = mapM_ (cgAlt scrutinee retVar) (zipIfElif $ map snd alts) 544 | 545 | cgDictCase :: LVar -> [(Const, Expr)] -> [Expr] -> Expr 546 | cgDictCase scrutinee items dflt = 547 | bigBraces (vcat $ punctuate comma items') 548 | <> case dflt of 549 | [] -> brackets $ cgVar scrutinee 550 | d:_ -> text ".get" <> parens (cgVar scrutinee <> comma <+> d) 551 | where 552 | items' = [ cgConst c <> colon <+> e | (c, e) <- items] 553 | 554 | -- Returns True iff the CG action generates no statements. 555 | isPureExpr :: CG Expr -> CG Bool 556 | isPureExpr (CG e) = CG $ do 557 | (stmts, expr) <- e 558 | return (empty, size stmts == 0) 559 | 560 | -- For case-expressions, we: 561 | -- 1. generate a fresh var 562 | -- 2. emit statements containing an if-elif-... chain that assigns to the var 563 | -- 3. use the assigned var as the expression standing for the result 564 | cgCase :: Bool -> LVar -> [DAlt] -> CG Expr 565 | cgCase tailPos var [DDefaultCase e] = cgExp tailPos e 566 | 567 | -- compile big constant-cases into dict lookups 568 | cgCase tailPos var alts 569 | | length alts > 8 570 | , all isConstant alts = do 571 | exprs <- mapM (cgExp False) [e | DConstCase c e <- alts] 572 | dflt <- mapM (cgExp False) [e | DDefaultCase e <- alts] 573 | return $ cgDictCase 574 | var 575 | (zip [c | DConstCase c e <- alts] exprs) 576 | dflt 577 | where 578 | isConstant :: DAlt -> Bool 579 | isConstant (DConstCase _ _) = True 580 | isConstant (DDefaultCase _) = True 581 | isConstant _ = False 582 | 583 | -- compile big constructor-cases into binary search on tags 584 | cgCase tailPos var alts 585 | | altCount >= 2 * groupSize -- there would be at least 2 full groups 586 | , DDefaultCase def : alts' <- reverse alts 587 | , all isConCase alts' = do 588 | taggedAlts <- sortBy (comparing fst) <$> mapM getTag alts' 589 | case tailPos of 590 | True -> do 591 | cgAltTree groupSize altCount Nothing var taggedAlts 592 | return $ cgError "unreachable due to case in tail position" 593 | False -> do 594 | retVar <- fresh 595 | cgAltTree groupSize altCount (Just retVar) var taggedAlts 596 | return $ cgVar retVar 597 | where 598 | groupSize = 3 -- smallest group size: (groupSize+1) `div` 2 599 | altCount = length alts 600 | 601 | isConCase :: DAlt -> Bool 602 | isConCase (DConCase _ _ _ _) = True 603 | isConCase _ = False 604 | 605 | getTag :: DAlt -> CG (Int, DAlt) 606 | getTag alt@(DConCase _ n _ _) = do 607 | Just tag <- ctorTag n 608 | return (tag, alt) 609 | 610 | -- otherwise just do the linear if-elif thing 611 | cgCase tailPos var alts 612 | | tailPos = do 613 | mapM_ (cgAlt var Nothing) (zipIfElif alts) 614 | return $ cgError "unreachable due to case in tail position" 615 | 616 | | not tailPos = do 617 | retVar <- fresh 618 | mapM_ (cgAlt var $ Just retVar) (zipIfElif alts) 619 | return $ cgVar retVar 620 | 621 | ifCond :: IfElif -> Expr -> Stmts 622 | ifCond If cond = text "if" <+> cond <> colon 623 | ifCond Elif cond = text "elif" <+> cond <> colon 624 | ifCond Else cond = text "else" <> colon 625 | ifCond Assert cond = text "assert" <+> cond 626 | 627 | indentCond :: IfElif -> CG () -> CG () 628 | indentCond Assert = id 629 | indentCond _ = sindent 630 | 631 | cgAlt :: LVar -> Maybe LVar -> (IfElif, DAlt) -> CG () 632 | cgAlt v retVar (ie, DConCase tag' ctorName args e) = do 633 | case special of 634 | -- normal constructors 635 | Nothing -> do 636 | -- DConCase does not contain useful tags yet 637 | -- we need to find out by looking up by name 638 | Just tag <- ctorTag ctorName 639 | emit $ ifCond ie (cgVar v <> text "[0] ==" <+> int tag) show ctorName 640 | 641 | -- special-cased constructors 642 | Just (ctor, test, match) -> 643 | emit $ ifCond ie (test $ cgVar v) show ctorName 644 | 645 | -- statements conditioned by the if 646 | indentCond ie $ do 647 | -- project out the args 648 | case args of 649 | [] -> return () 650 | _ -> emit $ case special of 651 | Nothing 652 | -> cgMatch (map Glob args) v 653 | Just (ctor, test, match) 654 | -> match (map cgName args) (cgVar v) 655 | 656 | -- evaluate the expression 657 | returnValue retVar e 658 | where 659 | special = specialCased ctorName 660 | 661 | cgAlt v retVar (ie, DConstCase c e) = do 662 | emit $ ifCond ie (cgVar v <+> text "==" <+> cgConst c) 663 | indentCond ie $ returnValue retVar e 664 | 665 | cgAlt v retVar (ie, DDefaultCase e) = do 666 | emit $ ifCond ie (text "True") -- the Bool will hopefully never be used 667 | indentCond ie $ returnValue retVar e 668 | 669 | returnValue :: Maybe LVar -> DExp -> CG () 670 | returnValue Nothing e = emit . (text "return" <+>) =<< cgExp True e -- we are in a tail position 671 | returnValue (Just r) e = emit . cgAssign r =<< cgExp False e -- we are not in a tail position 672 | 673 | -- special-cased constructors 674 | type SCtor = [Expr] -> Expr 675 | type STest = Expr -> Expr 676 | type SMatch = [Expr] -> Expr -> Expr 677 | 678 | specialCased :: Name -> Maybe (SCtor, STest, SMatch) 679 | specialCased n = lookup n 680 | -- Compile lists to a custom type that's iterable in Python (i.e. easy to call list() on). 681 | [ item "Prelude.List" "::" cons id uncons 682 | , item "Prelude.List" "Nil" nil falseTest nomatch 683 | 684 | -- Compile Idris booleans to Python booleans. 685 | , item "Prelude.Bool" "True" (\[] -> text "True") id nomatch 686 | , item "Prelude.Bool" "False" (\[] -> text "False") falseTest nomatch 687 | 688 | -- Compile (Just x) to (x) and Nothing to None. 689 | -- 690 | -- Warning: no other value is allowed to compile to "None"! 691 | -- 692 | -- If any value `n` of any type compiles to None, matching on `Just n` 693 | -- will take the `Nothing` branch, which is clearly incorrect. 694 | , item "Prelude.Maybe" "Just" (\[x] -> x) notNoneTest match 695 | , item "Prelude.Maybe" "Nothing" (\[] -> text "None") noneTest nomatch 696 | 697 | -- Due to the above, Unit must compile to a custom constant, not None. 698 | , item "" "MkUnit" unit noinspect nomatch 699 | , item "Builtins" "MkPair" tuple constTrue match 700 | ] 701 | where 702 | constTrue e = text "True" 703 | noneTest e = e <+> text "is None" 704 | notNoneTest e = e <+> text "is not None" 705 | falseTest e = text "not" <+> e 706 | nomatch args e = cgError $ show n ++ " should never be deconstructed" 707 | noinspect e = cgError $ show n ++ " should never be tested" 708 | 709 | unit [] = text "Unit" 710 | tuple args = parens (hsep $ punctuate comma args) 711 | cons [h,t] = t <> text ".cons" <> parens h 712 | nil [] = text "ConsList()" 713 | skip [x] = x 714 | 715 | uncons args e = match args (e <> text ".head" <> comma <+> e <> text ".tail") 716 | match args e = hsep (punctuate comma args) <+> text "=" <+> e 717 | 718 | -- Every item says: 719 | -- 1. what the namespace is 720 | -- 2. what the name is 721 | -- 3. how to construct the thing, given its arguments 722 | -- 4. what to put in the if-statement to test for the thing, given the expression to test 723 | -- 5. how to project fields from the thing 724 | item :: String -> String -> SCtor -> STest -> SMatch -> (Name, (SCtor, STest, SMatch)) 725 | item "" n ctor test match = (sUN n, (ctor, test, match)) 726 | item ns n ctor test match = (sNS (sUN n) (reverse $ split '.' ns), (ctor, test, match)) 727 | 728 | split :: Char -> String -> [String] 729 | split c "" = [""] 730 | split c (x : xs) 731 | | c == x = "" : split c xs 732 | | otherwise = let ~(h:t) = split c xs in ((x:h) : t) 733 | -------------------------------------------------------------------------------- /src/Main.hs: -------------------------------------------------------------------------------- 1 | module Main where 2 | 3 | import Idris.Core.TT 4 | import Idris.AbsSyntax 5 | import Idris.ElabDecls 6 | import Idris.REPL 7 | import Idris.Main 8 | import Idris.Options 9 | 10 | import IRTS.Compiler 11 | import IRTS.CodegenPython 12 | 13 | import System.Environment 14 | import System.Exit 15 | 16 | import Paths_idris_python 17 | 18 | data Opts = Opts { inputs :: [FilePath], 19 | output :: FilePath } 20 | 21 | showUsage = do putStrLn "Usage: idris-python [-o ]" 22 | exitWith ExitSuccess 23 | 24 | getOpts :: IO Opts 25 | getOpts = do xs <- getArgs 26 | return $ process (Opts [] "a.py") xs 27 | where 28 | process opts ("-o":o:xs) = process (opts { output = o }) xs 29 | process opts ("--yes-really":xs) = process opts xs -- TODO 30 | process opts (x:xs) = process (opts { inputs = x:inputs opts }) xs 31 | process opts [] = opts 32 | 33 | c_main :: Opts -> Idris () 34 | c_main opts = do elabPrims 35 | loadInputs (inputs opts) Nothing 36 | mainProg <- elabMain 37 | ir <- compile (Via IBCFormat "python") (output opts) (Just mainProg) 38 | runIO $ codegenPython ir 39 | 40 | main :: IO () 41 | main = do opts <- getOpts 42 | if (null (inputs opts)) 43 | then showUsage 44 | else runMain (c_main opts) 45 | 46 | 47 | -------------------------------------------------------------------------------- /src/Util/PrettyPrint.hs: -------------------------------------------------------------------------------- 1 | -- This module is equivalent to Text.PrettyPrint. 2 | -- The only difference is slightly different indentation behaviour. 3 | -- (Plus support of code comments). 4 | 5 | module Util.PrettyPrint 6 | ( Doc 7 | , int, text 8 | , comma, colon 9 | , lparen, rparen, lbracket, rbracket, lbrace, rbrace 10 | , (<>), (<+>), ($+$), ($$) 11 | , () 12 | , nest 13 | , parens, brackets 14 | , empty 15 | , render 16 | , vcat, hsep 17 | , punctuate 18 | , size, width 19 | ) 20 | where 21 | 22 | 23 | type Line = (String, String) -- text, comment 24 | newtype Doc = Doc [Line] 25 | instance Show Doc where 26 | show = render "--" 27 | 28 | infixr 6 <>, <+> 29 | infixr 5 $$, $+$ 30 | infixl 1 31 | 32 | int :: Int -> Doc 33 | int i = text $ show i 34 | 35 | text :: String -> Doc 36 | text s = Doc [(s, "")] 37 | 38 | comma, colon :: Doc 39 | comma = text "," 40 | colon = text ":" 41 | 42 | lparen, rparen, lbracket, rbracket, lbrace, rbrace :: Doc 43 | lparen = text "(" 44 | rparen = text ")" 45 | lbracket = text "[" 46 | rbracket = text "]" 47 | lbrace = text "{" 48 | rbrace = text "}" 49 | 50 | (<>) :: Doc -> Doc -> Doc 51 | Doc xs <> Doc ys = Doc $ meld "" xs ys 52 | 53 | (<+>) :: Doc -> Doc -> Doc 54 | Doc xs <+> Doc ys = Doc $ meld " " xs ys 55 | 56 | ($+$) :: Doc -> Doc -> Doc 57 | Doc xs $+$ Doc ys = Doc $ xs ++ ys 58 | 59 | ($$) :: Doc -> Doc -> Doc 60 | ($$) = ($+$) 61 | 62 | -- | Add a comment to the first line of the Doc. 63 | () :: Doc -> String -> Doc 64 | Doc [] comment = Doc [("", comment)] 65 | Doc ((t,c) : lines) comment = Doc $ (t, merge comment c) : lines 66 | where 67 | merge "" y = y 68 | merge x "" = x 69 | merge x y = x ++ " (" ++ y ++ ")" 70 | 71 | meld :: String -> [Line] -> [Line] -> [Line] 72 | meld sep [] ys = ys 73 | meld sep xs [] = xs 74 | meld sep [(x,xc)] ((y,yc) : ys) = (x ++ sep ++ y, merge xc yc) : ys 75 | where 76 | merge "" y = y 77 | merge x "" = x 78 | merge x y = x ++ ", " ++ y 79 | meld sep (x : xs) ys = x : meld sep xs ys 80 | 81 | nest :: Int -> Doc -> Doc 82 | nest n (Doc xs) = Doc [(replicate n ' ' ++ t, c) | (t, c) <- xs] 83 | 84 | parens :: Doc -> Doc 85 | parens d = lparen <> d <> rparen 86 | 87 | brackets :: Doc -> Doc 88 | brackets d = lbracket <> d <> rbracket 89 | 90 | render :: String -> Doc -> String 91 | render cmtStr (Doc xs) = unlines $ map (renderLine cmtStr) xs 92 | 93 | renderLine :: String -> (String, String) -> String 94 | renderLine cmtStr ("", "") = "" 95 | renderLine cmtStr ("", comment) = cmtStr ++ " " ++ comment 96 | renderLine cmtStr (content, "") = content 97 | renderLine cmtStr (content, comment) = content ++ " " ++ cmtStr ++ " " ++ comment 98 | 99 | empty :: Doc 100 | empty = Doc [] 101 | 102 | vcat :: [Doc] -> Doc 103 | vcat = foldr ($+$) empty 104 | 105 | hsep :: [Doc] -> Doc 106 | hsep = foldr (<+>) empty 107 | 108 | punctuate :: Doc -> [Doc] -> [Doc] 109 | punctuate sep [] = [] 110 | punctuate sep [x] = [x] 111 | punctuate sep (x : xs) = (x <> sep) : punctuate sep xs 112 | 113 | size :: Doc -> Int 114 | size (Doc xs) = sum [length t | (t, c) <- xs] 115 | 116 | width :: Doc -> Int 117 | width (Doc xs) = maximum [length t | (t, c) <- xs] 118 | -------------------------------------------------------------------------------- /stack.yaml: -------------------------------------------------------------------------------- 1 | resolver: lts-9.0 2 | 3 | packages: 4 | - location: . 5 | - location: 6 | git: git@github.com:idris-lang/Idris-dev 7 | commit: master 8 | extra-dep: true 9 | flags: 10 | idris: 11 | FFI: true 12 | GMP: true 13 | 14 | extra-deps: 15 | - binary-0.8.5.1 16 | - cheapskate-0.1.1 17 | 18 | nix: 19 | enable: false 20 | shell-file: stack-shell.nix 21 | --------------------------------------------------------------------------------