IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

Caml Discussion :

Réutilisation à l'aide de variants polymorphes


Sujet :

Caml

  1. #1
    Membre émérite
    Avatar de SpiceGuid
    Homme Profil pro
    Inscrit en
    Juin 2007
    Messages
    1 704
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Loire (Rhône Alpes)

    Informations forums :
    Inscription : Juin 2007
    Messages : 1 704
    Points : 2 990
    Points
    2 990
    Par défaut Réutilisation à l'aide de variants polymorphes
    J'ai un module Paths pour faire des chemins et des labyrinthes :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    module Paths
       =
    struct
    
       type path =
          [
          | `Exit
          | `North of path
          | `South of path
          | `East  of path
          | `West  of path
          | `Fork  of path * path
          ]
    
       let rec eval cont lex =
          let continue m =
             if m = `Exit then `Exit else eval cont lex m
          in function
          | `Fork(`Fork _,_) -> invalid_arg "Paths.eval"
          | (`North m | `Fork(`North m,_))
             when lex#granted_word("North") -> continue m  
          | (`South m | `Fork(`South m,_))
             when lex#granted_word("South") -> continue m
          | (`East  m | `Fork(`East  m,_))
             when lex#granted_word("East")  -> continue m
          | (`West  m | `Fork(`West  m,_))
             when lex#granted_word("West")  -> continue m
          | `Fork(_,m) -> eval cont lex m
          | m -> m
        
    end
    Ensuite j'ai un module Rooms pour ajouter des châteaux et des salles :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    module Rooms
       =
    struct
       
       type room =
          [
          | `Exit
          | `North of room
          | `South of room
          | `East  of room
          | `West  of room
          | `Fork  of room * room
          | `Room  of room * room
          ]
          
       let rec eval lex =
          function
          | #Paths.path as maze ->
                Paths.eval eval lex maze
          | `Room(inside,outside) when lex#granted_word("Enter") ->
                ignore(eval lex inside); eval lex outside
    
    end
    Mon problème:
    Le type qu'évalue Rooms.eval n'est pas égal (est plus restrictif) que mon type Rooms.room.
    Du même auteur: mon projet, le dernier article publié, le blog dvp et le jeu vidéo.
    Avant de poser une question je lis les règles du forum.

  2. #2
    Membre éprouvé
    Profil pro
    Inscrit en
    Avril 2007
    Messages
    832
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Avril 2007
    Messages : 832
    Points : 1 104
    Points
    1 104
    Par défaut
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
     
    type 'a path =
    [
    | `Exit
    | `Noth of 'a
    | ...
    ]
    Je ne comprends pas l'intérêt de passer ton argument `cont` qui n'est jamais utilisé.

  3. #3
    Membre émérite
    Avatar de SpiceGuid
    Homme Profil pro
    Inscrit en
    Juin 2007
    Messages
    1 704
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Loire (Rhône Alpes)

    Informations forums :
    Inscription : Juin 2007
    Messages : 1 704
    Points : 2 990
    Points
    2 990
    Par défaut Merci.
    je n'aurais sûrement pas trouvé tout seul
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    module Paths
       =
    struct
    
       type 'a path =
          [
          | `Exit
          | `North of 'a
          | `South of 'a
          | `East  of 'a
          | `West  of 'a
          | `Fork  of 'a * 'a
          ]
    
       let rec eval lex =
          let continue m =
             if m = `Exit then `Exit else eval lex m
          in function
          | `Fork(`Fork _,_) -> invalid_arg "Paths.eval"
          | (`North m | `Fork(`North m,_))
             when lex#granted_word("North") -> continue m  
          | (`South m | `Fork(`South m,_))
             when lex#granted_word("South") -> continue m
          | (`East  m | `Fork(`East  m,_))
             when lex#granted_word("East")  -> continue m
          | (`West  m | `Fork(`West  m,_))
             when lex#granted_word("West")  -> continue m
          | `Fork(_,m) -> eval lex m
          | m -> m
        
    end
    
    module Rooms
       =
    struct
    
       type 'a room = 
          [
          | `Exit
          | `North of 'a
          | `South of 'a
          | `East  of 'a
          | `West  of 'a
          | `Fork  of 'a * 'a
          | `Room  of 'a * 'a
          ]
          
       let rec eval lex =
          function
          | #Paths.path as maze ->
                Paths.eval lex maze
          | `Room(inside,outside) when lex#granted_word("Enter") ->
                ignore(eval lex inside); eval lex outside
          | m -> m
          
    end
    Du même auteur: mon projet, le dernier article publié, le blog dvp et le jeu vidéo.
    Avant de poser une question je lis les règles du forum.

+ Répondre à la discussion
Cette discussion est résolue.

Discussions similaires

  1. [F#] équivalent des Polymorphic variants de OCaML
    Par JohnT-47 dans le forum Caml
    Réponses: 1
    Dernier message: 04/10/2007, 12h47
  2. [OCaml] Variant polymorphes
    Par alex_pi dans le forum Caml
    Réponses: 1
    Dernier message: 12/09/2007, 23h22
  3. Besoin d'aide pour l'I.A. d'un puissance 4
    Par Anonymous dans le forum C
    Réponses: 2
    Dernier message: 25/04/2002, 17h05
  4. Une petite aide pour les API ?
    Par Yop dans le forum Windows
    Réponses: 2
    Dernier message: 04/04/2002, 21h45

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo