home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / lifeos2.zip / LIFE-1.02 / EXAMPLES / FLO_GRAM.LF < prev    next >
Text File  |  1996-06-04  |  18KB  |  597 lines

  1. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  2. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  3. %
  4. %                      LINDENMAYER SYSTEMS TRANSLATOR
  5. %
  6. %
  7. % The syntax of the rules is the following:
  8. %
  9. % Axiom ::> List of Symbols ?
  10. % Head ** Conditions ==>   List of Symbols 
  11. %
  12. % Applying one derivation step to a sequence of symbols results in replacing in
  13. % the sequence each symbol by the left hand side of a rule, provided that the
  14. % head of the rule and the symbol match. 
  15. % If a condition is attached to a rule, and if it is not satisfied, then the
  16. % rule cannot be used, and the system looks for another matching rule.
  17. % A set of rules with the same head may be declared as equi-probabilistic;
  18. % Symbols may have arguments; 
  19. %
  20. % A fixed number of derivations (see the complexity field in the interface) 
  21. % is applied to a sequence of symbols declared as the axiom of the L-system;
  22. % This results in a long sequence of symbols which are interpreted graphically.
  23. %
  24. % In this program, the interpretation is done on the fly, using the standard
  25. % derivation mechanism of Life. The interpretation of a symbol is specified by
  26. % declaring a predicate whose head is the symbol.
  27. %
  28. % This file provides a library of symbols (turtle symbols), that perform the
  29. % basic drawing operations.
  30. %
  31. % The method used to translate L-systems into Life code is not the most naive
  32. % one. We have tried to generate a code that, as far as possible, recovers
  33. % memory automatically, and is quite efficient. Each rule of the system is in
  34. % fact translated into three clauses (a naive implementation would generate
  35. % only one clause).
  36. %
  37. % This file is loaded automatically by the main file of the demo.
  38. % Author: Bruno Dumant
  39. %
  40. % Copyright 1992 Digital Equipment Corporation                                
  41. % All Rights Reserved                                                         
  42. %
  43. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  44. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  45.  
  46. module("rewrite_trans") ?
  47.  
  48. open("utils") ?
  49.  
  50. public( ==>,::>,**) ?
  51. op(1200,xfy,==>)?
  52. op(1200,xfy,::>)?
  53. op(1100,xfy,**)?
  54.  
  55. non_strict(**)?
  56.  
  57. %
  58. %  translation of axioms and rules 
  59. %
  60.  
  61. (Lhs ** Chs ==> Rhs) :-  !,
  62.     NewLhs = transSymb(Lhs,position=>head),
  63.     NewRhs = transSymb(Rhs,position=>body),!,
  64.     (prob_symb(root_sort(Lhs),_), !, T=true ; T=false ),
  65.     ( 
  66.           R1 = oneLevel(NewLhs,NewRhs,Chs,T),
  67.            assert(R1),
  68.            fail ;
  69.       R2 = twoLevel(NewLhs,NewRhs,Chs),
  70.                assert(R2),
  71.            fail ;
  72.       RN = nLevel(NewLhs,NewRhs,Chs,T),
  73.            assert(RN),
  74.            fail ;
  75.       succeed ).
  76.  
  77. (Lhs ==> Rhs) :-  
  78.     NewLhs = transSymb(Lhs,position=>head),
  79.     NewRhs = transSymb(Rhs,position=>body),!,
  80.     (prob_symb(root_sort(Lhs),_), !, T=true ; T=false ),
  81.     ( R1 = oneLevel(NewLhs,NewRhs,1,T),
  82.            assert(R1),
  83.            fail ;
  84.       R2 = twoLevel(NewLhs,NewRhs,1),
  85.                assert(R2),
  86.            fail ;
  87.       RN = nLevel(NewLhs,NewRhs,1,T),
  88.            assert(RN),
  89.            fail ;
  90.       succeed ).
  91.  
  92.  
  93. (Axiome ::> Rhs ) :-
  94.     Axiome.1 = N, 
  95.         R=newn(Rhs,N,_,_,_,_),
  96.     assert(( Axiome :- N>=2, R )).
  97.  
  98.                     
  99. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  100. %
  101. %  symbol translation
  102. %
  103. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  104.  
  105. %%% transSymb takes care of probabilistic rules: transSymb adds the appropriate
  106. %%% arguments to the symbols declared by probSymbol.
  107.  
  108. public(proba) ?
  109.  
  110. transSymb((Symbol,Others),position=>P) -> 
  111.     Symbol , transSymb(Others,position=>P)
  112.                         | Others :< @, !,
  113.               (prob_symb(Symbol,NumRules),!,     
  114.                Symbol.proba = `(random(NumRules)) ;
  115.                succeed ) .
  116. transSymb( Symbol,position=>P) -> 
  117.     Symbol | (prob_symb(Symbol,NumRules),!,
  118.               cond(P:==head,
  119.                (
  120.                I = index(Symbol),
  121.                I:@(Index),
  122.                Symbol.proba = Index, 
  123.                setPred(I,Index+1)
  124.                ),
  125.                Symbol.proba = `(random(NumRules))
  126.            );
  127.            succeed ) .
  128.  
  129. public(probSymbol) ?
  130. dynamic(prob_symb)?
  131.  
  132. probSymbol(Symbol,N) :- F=index(Symbol),
  133.                         setPred(F,0),
  134.                         M = N-1, 
  135.                         assert(prob_symb(X,M):- X :== Symbol ).
  136.  
  137. index(Symbol) -> str2psi(strcon("index",psi2str(Symbol))).
  138.  
  139. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  140.  
  141. %%% oneLevel rewrites (a ** cond ==> b,c) into (a1 :- cond, !, b, c) 
  142. %%% and adds the appropriate arguments.
  143.  
  144. oneLevel(Lhs ,Rhs, 1,true) -> 
  145.       ( new1h(Lhs,X,Y) :- !, new1b(Rhs,X,Y,[],S,[],S2) ).
  146. oneLevel(Lhs ,Rhs, 1,false) -> 
  147.       ( new1h(Lhs,X,Y) :- new1b(Rhs,X,Y,[],S,[],S2) ).
  148.  
  149. oneLevel(Lhs ,Rhs, Chs,B) -> 
  150.     ( new1h(Lhs,X,Y) :- Chs, !, new1b(Rhs,X,Y,[],S,[],S2) ).
  151.  
  152. public(oldState,newState) ?
  153. new1h(Symbol,X,Y) -> NS | NS=suffixRoot(Symbol,"1"),
  154.                           NS=strip(Symbol),
  155.                           NS.oldState = X,
  156.                           NS.newState = Y.
  157.  
  158. new1b((Symbol,Others),X,Y,S1,S3,SP1,SP3) -> 
  159.     new1b(Symbol,X,Z,S1,S2,SP1,SP2), 
  160.     new1b(Others,Z,Y,S2,S3,SP2,SP3).
  161.                          
  162. public(push,pop,startPol,endPol,dot) ?
  163.  
  164. new1b(push,X,Y,S1,S2,SP1,SP2) -> succeed | S2=[X|S1],Y=X,SP1=SP2.
  165. new1b(pop,X,Y,S1,S2,SP1,SP2) ->  succeed | S1=[Y|S2],SP1=SP2.
  166.  
  167. new1b(startPol,X,Y,S1,S2,SP1,SP2) -> 
  168.     succeed | S2=S1, Y=X, [[(X.coord.1,X.coord.2)]|SP1]=SP2.
  169. new1b(endPol,X,Y,S1,S2,SP1,SP2) -> 
  170.     endPol(L) | S2=S1, Y=X, SP1=[L|SP2].
  171. new1b(dot,X,Y,S1,S2,SP1,SP2) -> 
  172.     succeed | S2=S1, Y=X, SP1=[L|SP3], 
  173.                   SP2=[[(X.coord.1,X.coord.2)|L]|SP3].                                        
  174. new1b(Symbol,X,Y,S1,S2,SP1,SP2) -> NS | NS=root_sort(Symbol),
  175.                           NS=stripNoProba(Symbol),
  176.               NS.oldState = X,
  177.                           NS.newState = Y,
  178.                           S1=S2, 
  179.               SP1=SP2.
  180.  
  181. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  182.  
  183. %%% twoLevel rewrites (a ** cond ==> b,c) into 
  184. %%%                   (aN :- cond, !, ( b1, c1, fail ; succeed )),
  185. %%% and adds the appropriate arguments, and performing the right state
  186. %%% assignments (otherwise the turtle state would be lost on backtracking). The
  187. %%% reason for this is mainly memory recovery.
  188.  
  189. twoLevel(Lhs ,Rhs, 1) -> 
  190.     ( 
  191.         new2h(Lhs) :- !,
  192.         ( 
  193.         new2b(Rhs,`state,Y,[],S,[],S2), setq(`state,Y),
  194.         fail ; succeed 
  195.         )
  196.     ).
  197. twoLevel(Lhs ,Rhs, Chs) -> 
  198.     ( 
  199.         new2h(Lhs) :- Chs, !,
  200.         ( 
  201.         new2b(Rhs,`state,Y,[],S,[],S2), setq(`state,Y),
  202.         fail ; succeed 
  203.         )
  204.     ).
  205.                              
  206.  
  207. new2h(Symbol) -> NS | NS=suffixRoot(Symbol,"N"),
  208.                       NS=strip(Symbol),
  209.                       NS.rank=2.
  210.  
  211. new2b((Symbol,Others),X,Y,S1,S3,SP1,SP3) -> 
  212.     new2b(Symbol,X,Z,S1,S2,SP1,SP2), 
  213.     new2b(Others,Z,Y,S2,S3,SP2,SP3). 
  214.         
  215. new2b(push,X,Y,S1,S2,SP1,SP2) -> succeed | S2=[X|S1],Y=X,SP1=SP2.
  216. new2b(pop,X,Y,S1,S2,SP1,SP2) ->  succeed | S1=[Y|S2],SP1=SP2.
  217.  
  218. new2b(startPol,X,Y,S1,S2,SP1,SP2) -> 
  219.     succeed | S2=S1, Y=X, [[(X.coord.1,X.coord.2)]|SP1]=SP2.
  220. new2b(endPol,X,Y,S1,S2,SP1,SP2) -> 
  221.     endPol(L) | S2=S1, Y=X, SP1=[L|SP2].
  222. new2b(dot,X,Y,S1,S2,SP1,SP2) -> 
  223.     succeed | S2=S1, Y=X, SP1=[L|SP3], 
  224.                   SP2=[[(X.coord.1,X.coord.2)|L]|SP3].
  225.  
  226. new2b(Symbol,X,Y,S1,S2,SP1,SP2) -> NS 
  227.         | R=root_sort(Symbol),
  228.           ( turtleSymb(R), !, NS=R ; NS=suffixRoot(R,"1")),
  229.           NS=strip(Symbol),
  230.           NS.oldState = `(X),
  231.           NS.newState = Y,
  232.           S1=S2,
  233.           SP1=SP2.
  234.  
  235. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  236.  
  237. %%% twoLevel rewrites (a ** cond  ==> b,c) into 
  238. %%%             (aN(N) :- cond, !, ( M = N-1, bN(M), cN(M), fail ; succeed )) 
  239.  
  240.  
  241. nLevel(Lhs ,Rhs, 1,true) -> 
  242.     ( newn(Lhs,N,_,_,_,_) :- !,
  243.           ( `(M=N-1),newn(Rhs,M,[],_,[],_), fail ; succeed )).
  244. nLevel(Lhs ,Rhs, 1,false) -> 
  245.     ( newn(Lhs,N,_,_,_,_) :-
  246.           ( `(M=N-1),newn(Rhs,M,[],_,[],_), fail ; succeed )).
  247. nLevel(Lhs ,Rhs, Chs) -> 
  248.     ( newn(Lhs,N,_,_,_,_) :- Chs, !,
  249.           ( `(M=N-1),newn(Rhs,M,[],_,[],_), fail ; succeed )).
  250.  
  251.  
  252. newn((Symbol,Others),N,S1,S3,SP1,SP3) -> 
  253.     newn(Symbol,N,S1,S2,SP1,SP2), 
  254.     newn(Others,N,S2,S3,SP2,SP3).
  255.  
  256. newn(push,N,S1,S2,SP1,SP2) -> S2=[`state|S1],SP1=SP2.
  257. newn(pop,N,S1,S2,SP1,SP2) ->  S1=[Y|S2],setq(`state,Y),SP1=SP2.
  258.  
  259. newn(startPol,N,S1,S2,SP1,SP2) -> 
  260.     startPol(SP1,SP2) | S2=S1, Y=X. 
  261. newn(endPol,N,S1,S2,SP1,SP2) -> 
  262.     endPol(L) | S2=S1, Y=X, SP1=[L|SP2].
  263. newn(dot,N,S1,S2,SP1,SP2) -> 
  264.     dot(SP1,SP2)| S2=S1, Y=X.
  265.  
  266. newn(Symbol,N,S1,S2,SP1,SP2) -> NS 
  267.         | NS=suffixRoot(Symbol,"N"),
  268.           NS=strip(Symbol),
  269.           NS.rank=N,
  270.           S1=S2,
  271.           SP1=SP2.
  272.  
  273.  
  274. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  275.  
  276. %%% utility: get rid of the proba feature in a term
  277.  
  278. stripNoProba(Symbol) -> X | strNP(X,features(Symbol),Symbol).
  279.  
  280.  
  281. strNP(X,[],Symbol) :- !.
  282. strNP(X,[proba|L],Symbol) :- !.
  283. strNP(X,[A|L],Symbol) :- X.A = Symbol.A.
  284.  
  285.  
  286. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  287. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  288. %
  289. % L-Grammars : definition of "turtle" symbols
  290. %
  291. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  292. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  293.  
  294. %%% We implement here a 3D turtle:
  295. %%% fd(A) means translating the turtle of length d, and drawing the line;
  296. %%% fu(A) means translating the turtle of length d, and without drawing the
  297. %%%       line; 
  298. %%% u,l,h in the rp... and rm... symbols designate the three axes of the
  299. %%% turtle. rpX(A) means that a positive rotation of angle A is performed
  300. %%% around the X axe. rmX means that a fixed negative rotation of angle A is
  301. %%% performed around the X axe.
  302. %%%
  303. %%% turn means that the turtle goes in the opposite direction.
  304. %%%
  305. %%% decThick and setThick are not actually turtle symbols, but are used to
  306. %%% decrement or set the thickness of the lines drawn. setDefault is used to
  307. %%% reset the default color.
  308.  
  309. public(rpu,rmu,rpl,rml,rph,rmh,turn,fd,fu,decThick,setThick,setDefault) ?
  310. public(rpuN,rmuN,rplN,rmlN,rphN,rmhN,turnN,fdN,fuN,
  311.         decThickN,setThickN,setDefaultN) ?
  312. turtleSymb({rpu(A);rmu;rpl(A);rml;rph(A);rmh;turn;fd(A);fu(A);
  313.             decThick;setThick(A);setDefault}).
  314.  
  315. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  316. %
  317. % generation of the n-level definitions.
  318. %
  319. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  320.  
  321. genTurtleDefs :-  
  322.     turtleSymb(Symb), 
  323.     T = copy_pointer(Symb),
  324.     T.oldState=`state,
  325.     T.newState=NS,
  326.     NT=suffixRoot(Symb,"N"),
  327.         NT=strip(Symb),
  328.     NT.rank=N,
  329.     assert((NT :- ( T,
  330.                 setq(state,NS),fail ; succeed )) 
  331.                ),
  332.     fail .
  333.  
  334. genTurtleDefs.
  335.  
  336. genTurtleDefs ?
  337.  
  338. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  339. %
  340. % Basic definitions
  341. %
  342. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  343.  
  344. %
  345. %
  346. %  Rotations
  347. %
  348. %
  349.  
  350. rpu(A,oldState => turtleState(coord => C,
  351.                             direc => D:@(TH,TL,TU),
  352.                 thick => T ),
  353.     newState => NS) :- 
  354.         ( A:==@,!,Cos=cosDelta,Sin=sinDelta ;
  355.                   Cos=cos(A),Sin=sin(A)),
  356.     NS = turtleState(coord => C,
  357.                          direc => @(xmat(TH,TL,Cos,-Sin),
  358.                                     xmat(TH,TL,Sin,Cos),
  359.                     TU ),
  360.                          thick => T). 
  361.  
  362.  
  363. rmu(oldState => turtleState(coord => C,
  364.                             direc => D:@(TH,TL,TU),
  365.                 thick => T),
  366.     newState => NS ) :- 
  367.         @=@(Cos:cosDelta,Sin:sinDelta),
  368.     NS = turtleState(coord => C,
  369.                          direc => @(xmat(TH,TL,Cos,Sin),
  370.                                     xmat(TH,TL,-Sin,Cos),
  371.                     TU ),
  372.                          thick => T). 
  373.  
  374.  
  375.  
  376.  
  377. rpl(A,oldState => turtleState(coord => C,
  378.                             direc => D:@(TH,TL,TU),
  379.                 thick => T ),
  380.     newState => NS ) :- 
  381.     ( A:==@,!,Cos=cosDelta,Sin=sinDelta ;
  382.                   Cos=cos(A),Sin=sin(A)),
  383.             NS =turtleState(coord => C,
  384.                             direc => @(xmat(TH,TU,Cos,Sin),
  385.                                        TL,
  386.                        xmat(TH,TU,-Sin,Cos)),
  387.                 thick => T).               
  388.  
  389. rml(oldState => turtleState(coord => C,
  390.                                direc => D:@(TH,TL,TU),
  391.                    thick => T ),
  392.     newState => NS ) :- 
  393.     @=@(Cos:cosDelta,Sin:sinDelta),
  394.     NS =turtleState(coord => C,
  395.                     direc => @(xmat(TH,TU,Cos,-Sin),
  396.                                TL,
  397.                    xmat(TH,TU,Sin,Cos)),
  398.                     thick => T).                             
  399.  
  400.  
  401.  
  402.  
  403. rph(A,oldState => turtleState(coord => C,
  404.                                direc => D:@(TH,TL,TU),
  405.                    thick => T ),
  406.     newState => NS ) :- 
  407.     ( A:==@,!,Cos=cosDelta,Sin=sinDelta ;
  408.                   Cos=cos(A),Sin=sin(A)),
  409.     NS =turtleState(coord => C,
  410.                     direc => @(TH,
  411.                                    xmat(TL,TU,Cos,Sin),
  412.                    xmat(TL,TU,-Sin,Cos)),
  413.             thick => T).                       
  414.  
  415.  
  416.  
  417. rmh(oldState => turtleState(coord => C,
  418.                                direc => D:@(TH,TL,TU),
  419.                    thick => T,
  420.                                stack => S ),
  421.     newState => NS ) :- 
  422.     @=@(Cos:cosDelta,Sin:sinDelta),
  423.     NS =turtleState(coord => C,
  424.                     direc => @(TH,
  425.                                xmat(TL,TU,Cos,-Sin),
  426.                    xmat(TL,TU,Sin,Cos)),
  427.                     thick => T).              
  428.  
  429.  
  430.  
  431.  
  432. turn(oldState => turtleState(coord => C,
  433.                                direc => D:@(TH,TL,TU),
  434.                    thick => T ),
  435.      newState => NS ) :- 
  436.     NS =turtleState(coord => C,
  437.                         direc => @(xmat(TH,TL,-1,0),
  438.                                    xmat(TH,TL,0,-1),
  439.                    TU ),
  440.             thick => T).              
  441.  
  442. %
  443. % drawing
  444. %
  445.  
  446. public(draw_window,drFunc,drColor,realDistance) ?
  447. fd(R,
  448.    oldState => OS:turtleState(coord => @(X, Y, Z),
  449.                               direc => D:@(@(XH,YH,ZH),TL,TU),
  450.                   thick => T),
  451.    newState => NS ) :- 
  452.         cond( R:== @, R = realDistance ),
  453.     NS = turtleState( coord => @( X1: (X+R*XH),
  454.                                       Y1: (Y-R*YH),
  455.                       Z
  456.                                    ),
  457.               direc => D,
  458.               thick => T),
  459.     @ = @( DW:draw_window, DF:drFunc, DC:drColor),
  460.     xDrawLine(DW,X,Y,X1,Y1,color=>DC,function=>DF,linewidth=>floor(T)).
  461.  
  462.  
  463. fu(oldState => turtleState(coord => @(X, Y, Z),
  464.                            direc => D:@(@(XH,YH,ZH),TL,TU),
  465.                thick => T),
  466.    newState => NS ) :- 
  467.         (R:==@,!,R=realDistance ; succeed),
  468.     NS =turtleState( coord =>@(X1:(X+R*XH),
  469.                                    Y1:(Y-R*YH),
  470.                     %  Z1:(Z+R*ZH)
  471.                                    Z
  472.                            ),
  473.                          direc => D,
  474.              thick => T).
  475.  
  476.  
  477. %
  478. % decremente thick
  479. %
  480.  
  481. decThick(oldState => turtleState(coord => C,
  482.                                  direc => D,
  483.                      thick => T),
  484.          newState => NS ) :- 
  485.      NS =turtleState( coord => C,
  486.                           direc => D,
  487.               thick => T*0.702).
  488.  
  489. %
  490. % set Thick
  491. %
  492.  
  493. setThick(A,oldState => turtleState(coord => C,
  494.                                    direc => D,
  495.                        thick => T),
  496.            newState => NS ) :- 
  497.     NS =turtleState( coord => C,
  498.                          direc => D,
  499.              thick => A).
  500.  
  501.  
  502. %
  503. % Back to default values
  504. %
  505.  
  506. public(defaultColor) ?
  507.  
  508. setDefault( oldState => S , newState => S) :- 
  509.     setq(drColor,defaultColor).
  510.  
  511.  
  512.  
  513. %%% Symbols used to draw polygons.
  514.  
  515.  
  516. startPol(SP1,[[(X:(state.coord).1,X.2)]|SP1]).
  517.  
  518. endPol(L) :- xFillPolygon(draw_window,L,color=>drColor).
  519.  
  520. dot([L1|L2],[[(X:(state.coord).1,X.2)|L1]|L2]).
  521.  
  522.  
  523. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  524. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  525. %
  526. % L-Grammars : initialization predicates, and default values
  527. %
  528. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  529. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  530.  
  531. %%% Turtle's initial position
  532.  
  533. public(initPosition) ?
  534. initPosition(X,Y,Z:{0;real}) :- 
  535.     !,
  536.     S = state,
  537.     S.coord <- @(X,Y,Z),
  538.     setq(state,S).
  539.  
  540.  
  541. %%% Turtle's initial orientation
  542.  
  543. public(initDirection) ?
  544. initDirection(AH,AL,AU) :- 
  545.     S = state,
  546.     S.direc <- 
  547.        turnU(AU,turnH(AH,turnL(AL,@(@(0,1,0),@(-1,0,0),@(0,0,1))))),
  548.     setq(state,S).
  549.  
  550. turnU(AU,@(TH,TL,TU)) ->  @(xmat(TH,TL,Cos:cos(AU),-(Sin:sin(AU))),
  551.                           xmat(TH,TL,Sin,Cos),
  552.                           TU ).
  553. turnH(AH,@(TH,TL,TU)) ->  @(TH,
  554.                           xmat(TL,TU,Cos:cos(AH),Sin:sin(AH)),
  555.                           xmat(TL,TU,-Sin,Cos)).
  556. turnL(AL,@(TH,TL,TU)) ->  @(xmat(TH,TU,Cos:cos(AL),Sin:sin(AL)),
  557.                           TL,
  558.               xmat(TH,TU,-Sin,Cos)).
  559.  
  560.  
  561. %%% Turtle's initial color
  562.  
  563. public(initColor) ?
  564. initColor(C) :- setq(defaultColor,C),
  565.                 setq(drColor,C).
  566.  
  567.  
  568.  
  569. %%% Turtle's initial thickness
  570.  
  571. public(initThick) ?
  572. initThick(T) :- (X:state).thick <- T,
  573.                 setq(state,X).
  574.  
  575.  
  576. %%% Pre-Computing of cosinus and sinus
  577.  
  578. public(initAngle) ?
  579. public(delta) ?
  580. initAngle(Delta) :- setq(cosDelta,cos(Delta)),
  581.                     setq(sinDelta,sin(Delta)),
  582.                     setq(delta,Delta).
  583.  
  584.  
  585. %%% default values
  586.  
  587. initColor(xBlack)?
  588.  
  589. setq(cosDelta,0)?
  590. setq(sinDelta,1)?
  591. setq(realDistance,20)?
  592. setq(drFunc,xCopy) ?
  593. setq(state,turtleState( coord => @(0,0,0),
  594.                         direc => @(@(0,1,0),@(-1,0,0),@(0,0,1)),
  595.                         thick => 0 )) ?
  596.