definition module _SystemDynamic

from StdFile import ::Files, class FileSystem

import StdDynamicTypes
// exported functions:
writeDynamic :: String !Int Dynamic *f -> (Bool,*f) | FileSystem f

readDynamic :: String *f -> (Bool,Dynamic,*f) | FileSystem f


// Type of a dynamic (internal, change also convertDynamic.icl and overloading.icl)
// ----------------------------------------------------------------------------------------------------------------------
:: T_ypeObjectType
	= T_ypeConsSymbol T_ypeName T_ypeID [T_ypeObjectType]
	| P_laceholder (T_ypeObjectType -> T_ypeObjectType) (T_ypeObjectType -> T_ypeObjectType) T_ypeObjectType
	
:: T_ypeName	:== {#Char}		

:: T_ypeID
	// internal dynamic
	= ModuleID DummyModuleID				// within application
	
	// external dynamic
//	| DiskID !Int					// on disk (or other medium)
	| RunTimeID !Int				//	id (both encoded/decoded dynamic rts)
	| LazyRunTimeID !Int !Int 		// id {dynamic_index (both rt and disk)} {disk library instance w.r.t. main dynamic}
	
DynamicDefaultOptions	:== 0x00000000
HyperStrictEvaluation	:== 0x00000001

//_unify :: !T_ypeObjectType !T_ypeObjectType !ModuleQ -> (Bool, [T_ypeObjectType])
_unify :: !T_ypeObjectType !T_ypeObjectType -> (Bool, [T_ypeObjectType])
_coerce :: !T_ypeObjectType !T_ypeObjectType -> (Bool, [T_ypeObjectType])

_undo_indirections :: a ![T_ypeObjectType] -> a

:: DynamicTemp = E.a: {
		value	:: a
	,	type	:: T_ypeObjectType		// linker uses it!
	}
	
:: GlobalDynamicInfo
:: GlobalDynamicInfoDummy

/*
build_dynamic :: Int !GlobalDynamicInfoDummy -> Dynamic

copy_string_to_graph2 :: !String !Int !GlobalDynamicInfoDummy -> (.a,!Int)
*/

//copy_string_to_graph2 :: !String !Int !String !GlobalDynamicInfoDummy -> (.a,!Int)

// BuildBlock
:: Pointer

build_lazy_block :: !Int !Int -> Pointer;

build_block :: !Int !GlobalDynamicInfoDummy -> Pointer /* algemeen: een stuk graaf */

//build_block :: !Int !GlobalDynamicInfoDummy -> Pointer /* algemeen: een stuk graaf */
// John: Pointer should be changed in BuildBlockResult. I think it should left as it is
// because normally the user doesn't see this.

copy_string_to_graph2 :: !String !Int !String !GlobalDynamicInfoDummy !{#Int} !Int !Int -> (.a,!Int)

class EncodedDynamic a
where 
	dynamic_to_string :: !Int !Dynamic -> (!Bool,!*a)
		
instance EncodedDynamic String

//dynamic_to_string :: !Int !Dynamic -> (!Bool,!String);
string_to_dynamic :: !String -> (!Bool,!Dynamic)


//copy_graph_to_string :: a !*{#Int} !String -> (!String,!*{#Int});

copy_graph_to_string_new :: !Dynamic !{#Int} !String -> (!*{#Char},!*{#Int},!{String});

// -----------------------------------------------------------------
from StdDynamicLowLevelInterface import ::LazyDynamicReference;
:: Wrap a 
	= { 
		wrap_info		:: !a
	};

// aanpassen van gesharde type door alle library instanties		
:: *CopyGraphToStringArguments
	= {
		cgtsa_dynamic					:: Dynamic
	,	cgtsa_code_library_instances	:: !*{#Int}
	,	cgtsa_type_library_instances	:: !*{#Int}
	,	cgtsa_range_table				:: !{#Char}
	};
		
:: *CopyGraphToStringResults
	= {
		cgtsr_encoded_dynamic			:: !*{#Char}
	,	cgtsr_code_library_instances	:: !*{#Int}
	,	cgtsr_type_library_instances	:: !*{#Int}
	,	cgtsr_lazy_dynamic_references	:: !{#LazyDynamicReference}
	};

copy_graph_to_string_OK :: !(Wrap CopyGraphToStringArguments) -> !(Wrap CopyGraphToStringResults);

copy_graph_to_string_OK2 :: !(Wrap CopyGraphToStringArguments) -> !CopyGraphToStringResults;
