Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
| Download
GAP 4.8.9 installation with standard packages -- copy to your CoCalc project to get it
Project: cocalc-sagemath-dev-slelievre
Views: 418346/****************************************************************************1**2*W bool.c GAP source Martin Schönert3**4**5*Y Copyright (C) 1996, Lehrstuhl D für Mathematik, RWTH Aachen, Germany6*Y (C) 1998 School Math and Comp. Sci., University of St Andrews, Scotland7*Y Copyright (C) 2002 The GAP Group8**9** This file contains the functions for the boolean package.10**11** Note that boolean objects actually contain no data. The three of them12** are distinguished by their addresses, kept in the C globals False,13** True and Fail.14*/15#include "system.h" /* system dependent part */161718#include "gasman.h" /* garbage collector */19#include "objects.h" /* objects */20#include "scanner.h" /* scanner */2122#include "gap.h" /* error handling, initialisation */2324#include "gvars.h" /* global variables */25#include "calls.h" /* generic call mechanism */26#include "opers.h" /* generic operations */2728#include "ariths.h" /* basic arithmetic */2930#include "bool.h" /* booleans */3132#include "records.h" /* generic records */33#include "precord.h" /* plain records */3435#include "lists.h" /* generic lists */36#include "string.h" /* strings */3738#include "code.h" /* coder */39#include "thread.h" /* threads */40#include "tls.h" /* thread-local storage */414243/****************************************************************************44**4546*V True . . . . . . . . . . . . . . . . . . . . . . . . . . . . true value47**48** 'True' is the value 'true'.49*/50Obj True;515253/****************************************************************************54**55*V False . . . . . . . . . . . . . . . . . . . . . . . . . . . . false value56**57** 'False' is the value 'false'.58*/59Obj False;606162/****************************************************************************63**64*V Fail . . . . . . . . . . . . . . . . . . . . . . . . . . . . fail value65**66** 'Fail' is the value 'fail'.67*/68Obj Fail;6970/****************************************************************************71**72*V SuPeRfail . . . . . . . . . . . . . . . . . . . . . . . superfail value73**74** 'SuPeRfail' is an ``superfail'' object which is used to indicate failure if75** `fail' itself is a sensible response. This is used when having GAP read76** a file line-by-line via a library function (demo.g)77*/78Obj SuPeRfail;798081/****************************************************************************82**8384*F TypeBool( <bool> ) . . . . . . . . . . . . . . . type of a boolean value85**86** 'TypeBool' returns the type of boolean values.87**88** 'TypeBool' is the function in 'TypeObjFuncs' for boolean values.89*/90Obj TYPE_BOOL;9192Obj TypeBool (93Obj val )94{95return TYPE_BOOL;96}979899/****************************************************************************100**101*F PrintBool( <bool> ) . . . . . . . . . . . . . . . . print a boolean value102**103** 'PrintBool' prints the boolean value <bool>.104*/105void PrintBool (106Obj bool )107{108if ( bool == True ) {109Pr( "true", 0L, 0L );110}111else if ( bool == False ) {112Pr( "false", 0L, 0L );113}114else if ( bool == Fail ) {115Pr( "fail", 0L, 0L );116}117else if ( bool == SuPeRfail ) {118Pr( "SuPeRfail", 0L, 0L );119}120else {121Pr( "<<very strange boolean value>>", 0L, 0L );122}123}124125126/****************************************************************************127**128*F EqBool( <boolL>, <boolR> ) . . . . . . . . . test if <boolL> = <boolR>129**130** 'EqBool' returns 'True' if the two boolean values <boolL> and <boolR> are131** equal, and 'False' otherwise.132*/133Int EqBool (134Obj boolL,135Obj boolR )136{137if ( boolL == boolR ) {138return 1L;139}140else {141return 0L;142}143}144145146/****************************************************************************147**148*F LtBool( <boolL>, <boolR> ) . . . . . . . . . test if <boolL> < <boolR>149**150** The ordering of Booleans is true < false <= fail (the <= comes from151** the fact that Fail may be equal to False in some compatibility modes152*/153Int LtBool (154Obj boolL,155Obj boolR )156{157return ( boolL == True && boolR != True) ||158( boolL == False && boolR == Fail && boolL != boolR);159}160161162/****************************************************************************163**164*F IsBoolFilt( <self>, <obj> ) . . . . . . . . . . test for a boolean value165**166** 'IsBoolFilt' implements the internal filter 'IsBool'.167**168** 'IsBool( <obj> )'169**170** 'IsBool' returns 'true' if <obj> is a boolean value and 'false'171** otherwise.172*/173Obj IsBoolFilt;174175Obj IsBoolHandler (176Obj self,177Obj obj )178{179/* return 'true' if <obj> is a boolean and 'false' otherwise */180if ( TNUM_OBJ(obj) == T_BOOL ) {181return True;182}183else if ( TNUM_OBJ(obj) < FIRST_EXTERNAL_TNUM ) {184return False;185}186else {187return DoFilter( self, obj );188}189}190191192/****************************************************************************193**194195*F ReturnTrue1( <val1> ) . . . . . . . . . . . . . . . . . . return 'True'196**197** 'ReturnTrue?' simply return 'True' independent of the values of the198** arguments.199**200** Those functions are useful for dispatcher tables if the types already201** determine the outcome.202*/203Obj ReturnTrue1 (204Obj self,205Obj val1 )206{207return True;208}209210211/****************************************************************************212**213*F ReturnTrue2( <val1>, <val2> ) . . . . . . . . . . . . . . return 'True'214*/215Obj ReturnTrue2 (216Obj self,217Obj val1,218Obj val2 )219{220return True;221}222223224/****************************************************************************225**226*F ReturnTrue3( <val1>, <val2>, <val3> ) . . . . . . . . . . return 'True'227*/228Obj ReturnTrue3 (229Obj self,230Obj val1,231Obj val2,232Obj val3 )233{234return True;235}236237238/****************************************************************************239**240*F ReturnFalse1( <val1> ) . . . . . . . . . . . . . . . . . return 'False'241**242** 'ReturnFalse?' likewise return 'False'.243*/244Obj ReturnFalse1 (245Obj self,246Obj val1 )247{248return False;249}250251252/****************************************************************************253**254*F ReturnFalse2( <val1>, <val2> ) . . . . . . . . . . . . . return 'False'255*/256Obj ReturnFalse2 (257Obj self,258Obj val1,259Obj val2 )260{261return False;262}263264265/****************************************************************************266**267*F ReturnFalse3( <val1>, <val2>, <val3> ) . . . . . . . . . return 'False'268*/269Obj ReturnFalse3 (270Obj self,271Obj val1,272Obj val2,273Obj val3 )274{275return False;276}277278279/****************************************************************************280**281*F ReturnFail1( <val1> ) . . . . . . . . . . . . . . . . . . return 'Fail'282**283** 'ReturnFail?' likewise return 'Fail'.284*/285Obj ReturnFail1 (286Obj self,287Obj val1 )288{289return Fail;290}291292293/****************************************************************************294**295*F ReturnFail2( <val1>, <val2> ) . . . . . . . . . . . . . . return 'Fail'296*/297Obj ReturnFail2 (298Obj self,299Obj val1,300Obj val2 )301{302return Fail;303}304305306/****************************************************************************307**308*F ReturnFail3( <val1>, <val2>, <val3> ) . . . . . . . . . . return 'Fail'309*/310Obj ReturnFail3 (311Obj self,312Obj val1,313Obj val2,314Obj val3 )315{316return Fail;317}318319320/****************************************************************************321**322323*F SaveBool( <bool> ) . . . . . . . . . . . . . . . . . . . . save a Boolean324**325** Actually, there is nothing to do326*/327328void SaveBool( Obj obj )329{330return;331}332333/****************************************************************************334**335*F LoadBool( <bool> ) . . . . . . . . . . . . . . . . . . . . save a Boolean336**337** Actually, there is nothing to do338*/339340void LoadBool( Obj obj )341{342return;343}344345/****************************************************************************346**347348*F * * * * * * * * * * * * * initialize package * * * * * * * * * * * * * * *349*/350351/****************************************************************************352**353354*V GVarFilts . . . . . . . . . . . . . . . . . . . list of filters to export355*/356static StructGVarFilt GVarFilts [] = {357358{ "IS_BOOL", "obj", &IsBoolFilt,359IsBoolHandler, "src/bool.c:IS_BOOL" },360361{ 0 }362363};364365366/****************************************************************************367**368369*F InitKernel( <module> ) . . . . . . . . initialise kernel data structures370*/371static Int InitKernel (372StructInitInfo * module )373{374/* install the marking functions for boolean values */375InfoBags[ T_BOOL ].name = "boolean or fail";376InitMarkFuncBags( T_BOOL, MarkNoSubBags );377378/* init filters and functions */379InitHdlrFiltsFromTable( GVarFilts );380381/* make and install the 'RETURN_TRUE' function */382InitHandlerFunc( ReturnTrue1, "src/bool.c:ReturnTrue1" );383InitHandlerFunc( ReturnTrue2, "src/bool.c:ReturnTrue2" );384InitHandlerFunc( ReturnTrue3, "src/bool.c:ReturnTrue3" );385386/* make and install the 'RETURN_FALSE' function */387InitHandlerFunc( ReturnFalse1, "src/bool.c:ReturnFalse1" );388InitHandlerFunc( ReturnFalse2, "src/bool.c:ReturnFalse2" );389InitHandlerFunc( ReturnFalse3, "src/bool.c:ReturnFalse3" );390391/* make and install the 'RETURN_FAIL' function */392InitHandlerFunc( ReturnFail1, "src/bool.c:ReturnFail1" );393InitHandlerFunc( ReturnFail2, "src/bool.c:ReturnFail2" );394InitHandlerFunc( ReturnFail3, "src/bool.c:ReturnFail3" );395396/* install the type function */397ImportGVarFromLibrary( "TYPE_BOOL", &TYPE_BOOL );398TypeObjFuncs[ T_BOOL ] = TypeBool;399400/* make the boolean bags */401InitGlobalBag( &True, "src/bool.c:TRUE" );402InitGlobalBag( &False, "src/bool.c:FALSE" );403InitGlobalBag( &Fail, "src/bool.c:FAIL" );404InitGlobalBag( &SuPeRfail, "src/bool.c:SUPERFAIL" );405406/* install the saving functions */407SaveObjFuncs[ T_BOOL ] = SaveBool;408409/* install the loading functions */410LoadObjFuncs[ T_BOOL ] = LoadBool;411412/* install the printer for boolean values */413PrintObjFuncs[ T_BOOL ] = PrintBool;414415/* install the comparison functions */416EqFuncs[ T_BOOL ][ T_BOOL ] = EqBool;417LtFuncs[ T_BOOL ][ T_BOOL ] = LtBool;418419/* return success */420return 0;421}422423424/****************************************************************************425**426*F InitLibrary( <module> ) . . . . . . . initialise library data structures427*/428static Int InitLibrary (429StructInitInfo * module )430{431UInt gvar;432Obj tmp;433434/* init filters and functions */435InitGVarFiltsFromTable( GVarFilts );436437/* bags are registered in 'InitKernel' */438True = NewBag( T_BOOL, 0L );439False = NewBag( T_BOOL, 0L );440Fail = NewBag( T_BOOL, 0L );441442/* `fail' is a variable not a language construct */443gvar = GVarName( "fail" );444AssGVar( gvar, Fail );445MakeReadOnlyGVar(gvar);446447/* `SuPeRfail' ditto */448SuPeRfail = NewBag( T_BOOL, 0L );449gvar = GVarName( "SuPeRfail" );450AssGVar( gvar, SuPeRfail );451MakeReadOnlyGVar(gvar);452453/* make and install the 'RETURN_TRUE' function */454tmp = NewFunctionC( "RETURN_TRUE", -1L, "arg", ReturnTrue1 );455HDLR_FUNC( tmp, 1 ) = ReturnTrue1;456HDLR_FUNC( tmp, 2 ) = ReturnTrue2;457HDLR_FUNC( tmp, 3 ) = ReturnTrue3;458AssGVar( GVarName("RETURN_TRUE"), tmp );459460/* make and install the 'RETURN_FALSE' function */461tmp = NewFunctionC("RETURN_FALSE",-1L,"arg",ReturnFalse1);462HDLR_FUNC( tmp, 1 ) = ReturnFalse1;463HDLR_FUNC( tmp, 2 ) = ReturnFalse2;464HDLR_FUNC( tmp, 3 ) = ReturnFalse3;465AssGVar( GVarName( "RETURN_FALSE" ), tmp );466467/* make and install the 'RETURN_FAIL' function */468tmp = NewFunctionC("RETURN_FAIL", -1L, "arg", ReturnFail1);469HDLR_FUNC( tmp, 1 ) = ReturnFail1;470HDLR_FUNC( tmp, 2 ) = ReturnFail2;471HDLR_FUNC( tmp, 3 ) = ReturnFail3;472AssGVar( GVarName( "RETURN_FAIL" ), tmp );473474/* return success */475return 0;476}477478479/****************************************************************************480**481*F InitInfoBool() . . . . . . . . . . . . . . . . . table of init functions482*/483static StructInitInfo module = {484MODULE_BUILTIN, /* type */485"bool", /* name */4860, /* revision entry of c file */4870, /* revision entry of h file */4880, /* version */4890, /* crc */490InitKernel, /* initKernel */491InitLibrary, /* initLibrary */4920, /* checkInit */4930, /* preSave */4940, /* postSave */4950 /* postRestore */496};497498StructInitInfo * InitInfoBool ( void )499{500return &module;501}502503504/****************************************************************************505**506507*E bool.c . . . . . . . . . . . . . . . . . . . . . . . . . . . . ends here508*/509510511