|
| 1 | +#! @Chapter Examples and tests |
| 2 | + |
| 3 | +#! @Section Tests |
| 4 | + |
| 5 | +LoadPackage( "LinearAlgebraForCAP" ); |
| 6 | + |
| 7 | +#! @Example |
| 8 | + |
| 9 | +Q := HomalgFieldOfRationals();; |
| 10 | +rows := MatrixCategory( Q );; |
| 11 | + |
| 12 | +MyKernelLift := function( cat, mor, test_morphism ) |
| 13 | + #% CAP_JIT_NEXT_FUNCCALL_DOES_NOT_RETURN_FAIL |
| 14 | + return Lift( cat, test_morphism, KernelEmbedding( cat, mor ) ); end;; |
| 15 | + |
| 16 | +V := VectorSpaceObject( 2, Q );; |
| 17 | +compiled_func := CapJitCompiledFunction( |
| 18 | + MyKernelLift, |
| 19 | + [ rows, ZeroMorphism( V, V ), IdentityMorphism( V ) ] |
| 20 | +);; |
| 21 | +tree := SYNTAX_TREE( compiled_func );; |
| 22 | +# check that the fail has been compiled away, |
| 23 | +# that is, we simply return a CAP morphism |
| 24 | +Length( tree.stats.statements ) = 1; |
| 25 | +#! true |
| 26 | +tree.stats.statements[1].type = "STAT_RETURN_OBJ"; |
| 27 | +#! true |
| 28 | +StartsWith( tree.stats.statements[1].obj.type, "EXPR_FUNCCALL" ); |
| 29 | +#! true |
| 30 | +tree.stats.statements[1].obj.funcref.type = "EXPR_REF_GVAR"; |
| 31 | +#! true |
| 32 | +tree.stats.statements[1].obj.funcref.gvar = "ObjectifyWithAttributes"; |
| 33 | +#! true |
| 34 | + |
| 35 | +func1 := function( x ) |
| 36 | + #% CAP_JIT_RESOLVE_FUNCTION |
| 37 | + if x = 1 then return fail; fi; return 1; end;; |
| 38 | + |
| 39 | +func2 := function( x ) |
| 40 | + #% CAP_JIT_RESOLVE_FUNCTION |
| 41 | + if x = 1 then return fail; else return 1; fi; end;; |
| 42 | + |
| 43 | +# we have to work hard to not write semicolons so AutoDoc |
| 44 | +# does not begin a new statement |
| 45 | +func3 := EvalString( ReplacedString( """function( x ) |
| 46 | + local y@ |
| 47 | + #% CAP_JIT_RESOLVE_FUNCTION |
| 48 | + if x = 1 then |
| 49 | + y := fail@ |
| 50 | + else |
| 51 | + y := 1@ |
| 52 | + fi@ |
| 53 | + return y@ |
| 54 | +end""", "@", ";" ) );; |
| 55 | + |
| 56 | +call_func1 := function( x ) |
| 57 | + #% CAP_JIT_NEXT_FUNCCALL_DOES_NOT_RETURN_FAIL |
| 58 | + return func1( x ); end;; |
| 59 | + |
| 60 | +call_func2 := function( x ) |
| 61 | + #% CAP_JIT_NEXT_FUNCCALL_DOES_NOT_RETURN_FAIL |
| 62 | + return func2( x ); end;; |
| 63 | + |
| 64 | +call_func3 := function( x ) |
| 65 | + #% CAP_JIT_NEXT_FUNCCALL_DOES_NOT_RETURN_FAIL |
| 66 | + return func3( x ); end;; |
| 67 | + |
| 68 | +Display( CapJitCompiledFunction( call_func1, [ 2 ] ) ); |
| 69 | +#! function ( x ) |
| 70 | +#! return 1; |
| 71 | +#! end |
| 72 | +Display( CapJitCompiledFunction( call_func2, [ 2 ] ) ); |
| 73 | +#! function ( x ) |
| 74 | +#! return 1; |
| 75 | +#! end |
| 76 | +Display( CapJitCompiledFunction( call_func3, [ 2 ] ) ); |
| 77 | +#! function ( x ) |
| 78 | +#! return 1; |
| 79 | +#! end |
| 80 | + |
| 81 | +#! @EndExample |
0 commit comments